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;
相关推荐
客卿1234 小时前
用两个栈实现队列
android·java·开发语言
代码探秘者4 小时前
【算法】吃透18种Java 算法快速读写模板
数据结构·数据库·python·算法·spring
java1234_小锋4 小时前
Java高频面试题:谈谈你对SpringBoot的理解?
java·开发语言·spring boot
空空潍4 小时前
Spring AI 实战系列(三):多模型共存+双版本流式输出
java·人工智能·spring
彭于晏Yan5 小时前
SpringBoot整合ECC实现文件签名与验签
java·spring boot·后端
pupudawang5 小时前
Spring EL 表达式的简单介绍和使用
java·后端·spring
jiankeljx5 小时前
Spring Initializr创建springboot项目,提示java 错误 无效的源发行版:16
java·spring boot·spring
competes5 小时前
深圳程序员职业生涯
java·大数据·开发语言·人工智能·java-ee
深蓝轨迹5 小时前
Redis 消息队列
java·数据库·redis·缓存·面试·秒杀
小小小米粒5 小时前
Collection(单列集合)、Map(双列集合),容易搞混的 Collections 工具类。
java·开发语言