Spring 容器相关的核心注解

以下是 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 容器的核心注解​

  1. ​Bean 注册​@Component, @Service, @Bean, @Configuration
  2. ​依赖注入​@Autowired, @Resource, @Value
  3. ​条件控制​@Profile, @Conditional, @Scope
  4. ​生命周期​@PostConstruct, @PreDestroy
  5. ​配置管理​@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;
相关推荐
智慧物业老杨7 小时前
物业数字化落地思考:真正的转型,是底层数据秩序的重构
java·大数据·人工智能·微服务·系统架构
上进小菜猪10 小时前
把 KES 集群交给 Kubernetes 的九十天:一个 DBA 的试用实录
后端
星空10 小时前
金蝶苍穹build.gradle配置
java·build.gradle
两点王爷11 小时前
PostgreSQL 常用 SQL 语句与 GIS 相关函数详解
数据库·后端
考虑考虑11 小时前
Springboot环境变量占位符语法
spring boot·后端·spring
步行cgn13 小时前
Spring 基于 XML 的自动装配:byName 详解
java·后端·spring
不会c+13 小时前
Day02 - 需求分析与用例建模
spring
大帅点兵13 小时前
Apache Ant 1.9.9 完整讲解
java
LuTshoes14 小时前
spring ai 实战 手搓 PlaneExecuteAgent
java·人工智能·spring·ai
这料鬼有毒14 小时前
二刷hot100-73.矩阵置零
java