以下是 Spring 容器中用于 Bean 管理、依赖注入、配置控制 的关键注解,按功能分类说明:
1. Bean 声明与注册
注解 | 作用 | 示例 |
---|---|---|
@Component |
通用注解,标记一个类为 Spring Bean(自动扫描注册) | @Component public class UserService { ... } |
@Service |
专用于业务逻辑层(功能同 @Component ,但语义更清晰) |
@Service public class OrderService { ... } |
@Repository |
专用于数据访问层(DAO),自动转换数据库异常为 Spring 统一异常 | @Repository public class UserDao { ... } |
@Controller |
专用于 MVC 控制器层(处理 HTTP 请求) | @Controller public class UserController { ... } |
@RestController |
@Controller + @ResponseBody (直接返回 JSON/XML) |
@RestController public class ApiController { ... } |
@Configuration |
标记类为配置类(定义 @Bean 方法) |
@Configuration public class AppConfig { ... } |
@Bean |
在配置类中显式声明 Bean(适用于第三方库或复杂对象) | @Bean public RestTemplate restTemplate() { return new RestTemplate(); } |
2. 依赖注入(DI)
注解 | 作用 | 示例 |
---|---|---|
@Autowired |
自动注入依赖(默认按类型匹配,可结合 @Qualifier 按名称匹配) |
@Autowired private UserService userService; |
@Qualifier |
指定具体注入的 Bean 名称(解决多个同类型 Bean 冲突) | @Autowired @Qualifier("masterDataSource") private DataSource ds; |
@Resource |
JSR-250 标准注解,按名称注入(类似 @Autowired + @Qualifier ) |
@Resource(name = "slaveDataSource") private DataSource ds; |
@Value |
注入配置文件中的属性或简单值 | @Value("${app.timeout}") private int timeout; |
3. 条件化与作用域控制
注解 | 作用 | 示例 |
---|---|---|
@Scope |
定义 Bean 的作用域(如单例、原型、会话等) | @Scope("prototype") public class Task { ... } |
@Lazy |
延迟初始化 Bean(首次使用时才创建) | @Lazy @Service public class HeavyService { ... } |
@Conditional |
根据条件决定是否注册 Bean(需实现 Condition 接口) |
@Conditional(OnDevEnvCondition.class) @Bean public DevTool devTool() { ... } |
@Profile |
指定 Bean 在特定环境(如 dev/test/prod)下生效 | @Profile("prod") @Bean public DataSource prodDataSource() { ... } |
4. 生命周期回调
注解 | 作用 | 示例 |
---|---|---|
@PostConstruct |
Bean 初始化后执行(相当于 init-method ) |
@PostConstruct public void init() { ... } |
@PreDestroy |
Bean 销毁前执行(相当于 destroy-method ) |
@PreDestroy public void cleanup() { ... } |
5. 配置与扫描控制
注解 | 作用 | 示例 |
---|---|---|
@ComponentScan |
指定 Spring 扫描 Bean 的包路径(用于启动类或配置类) | @ComponentScan("com.example") public class App { ... } |
@Import |
导入其他配置类或普通类(使其成为 Spring Bean) | @Import({DatabaseConfig.class, SecurityConfig.class}) |
@PropertySource |
加载外部配置文件(如 application.properties ) |
@PropertySource("classpath:custom.properties") |
6. AOP 相关注解
注解 | 作用 | 示例 |
---|---|---|
@Aspect |
声明一个切面类 | @Aspect @Component public class LoggingAspect { ... } |
@Before /@After |
定义通知(Advice)在目标方法执行前/后运行 | @Before("execution(* com.example.service.*.*(..))") |
@Around |
环绕通知(可控制目标方法是否执行) | @Around("@annotation(com.example.LogExecutionTime)") |
7. 测试相关注解
注解 | 作用 | 示例 |
---|---|---|
@SpringBootTest |
启动完整的 Spring 容器进行集成测试 | @SpringBootTest class UserServiceTest { ... } |
@MockBean |
向容器注入 Mock 对象(替代真实 Bean) | @MockBean private UserRepository userRepository; |
总结:Spring 容器的核心注解
- Bean 注册 :
@Component
,@Service
,@Bean
,@Configuration
- 依赖注入 :
@Autowired
,@Resource
,@Value
- 条件控制 :
@Profile
,@Conditional
,@Scope
- 生命周期 :
@PostConstruct
,@PreDestroy
- 配置管理 :
@ComponentScan
,@PropertySource
,@Import
补充(与内容主题无关):
如果有一个service接口被2个类实现了,那么只能用
@Resource/@Autowired+@Qualifier
@Service("timeQuantumServiceA")
public class TimeQuantumServiceImplA implements TimeQuantumService {}
@Service("timeQuantumServiceB")
public class TimeQuantumServiceImplB implements TimeQuantumService {}
@Autowired
@Qualifier("timeQuantumServiceA") // 必须指定名称
private TimeQuantumService timeQuantumService;