SPRINGBOOT学习之注解
1.@ConditionalOnBean
用于条件注册Bean的注解,当指定的Bean存在时,当前Bean才会被创建
核心作用
按需加载:仅在依赖的Bean存在时注册当前Bean,避免冗余创建 。
模块化设计:控制功能模块的启用条件,例如数据库相关Bean仅在DataSource存在时创建 。
避免冲突:确保Bean依赖满足时才加载,防止因缺少依赖导致的初始化失败 。
使用场景
数据库服务:若容器中存在DataSource,则创建DatabaseService 。
自动配置:Spring Boot的MVC自动配置仅在DispatcherServlet存在时生效 。
可选组件:如Redis缓存管理器仅在RedisTemplate存在时加载 。
点击查看代码
@Configuration
public class MyConfig {
@Bean
public DataSource dataSource() {
return new FakeDataSource();
}
@Bean
@ConditionalOnBean(DataSource.class)
public MyService myService() {
return new MyService();
}
}
2.@ConditionalOnMissingBean
根据容器中是否不存在某个特定的 Bean 来决定是否注册当前的 Bean。通常用于提供默认配置或备用实现。
点击查看代码
@Configuration
public class BeanConfig {
@Bean(name = "notebookPC")
public Computer computer1() {
return new Computer("笔记本电脑");
}
@ConditionalOnMissingBean(Computer.class)
@Bean("reservePC")
public Computer computer2() {
return new Computer("备用电脑");
}
}
3.@Bean
参考https://cloud.tencent.com/developer/article/2442805
定义和管理Spring容器中的Bean的核心工具之一。通过使用@Bean注解,开发者可以声明方法,并将其返回值作为Spring容器中的一个Bean。这使得Spring容器能够管理这些对象的生命周期,并在需要时进行依赖注入。
@Bean注解的实现依赖于Spring的注解驱动的配置机制。当Spring容器启动时,它会扫描带有@Configuration注解的类,并调用其中带有@Bean注解的方法,将返回值注册为Bean。
点击查看代码
@Configuration
public class MiddlewareConfig {
@Bean
public MessageQueue messageQueue() {
// 创建并返回消息队列实例
return new SomeMessageQueueImplementation();
}
}
@Configuration
public class MiddlewareConfig {
@Bean
public MessageQueue messageQueue() {
// 创建实例
AImpl impl = new AImpl();
//impl设置各种属性,初始化
impl.setProperty(a,b,c);
return impl;
}
}
https://cloud.tencent.com/developer/article/2572196
4.@Configuration
Spring框架中用于标记配置类的关键注解,其核心作用是确保配置类中的Bean方法被代理,从而支持单例模式和依赖注入的正确行为。
在 Spring Boot 开发中,配置类是替代传统 XML 配置的核心载体,通过一系列注解可实现 Bean 注册、属性绑定、条件装配等关键功能。
与@Component的区别:@Configuration本质是@Component的子类,但前者更强调 “配置功能”,且支持 proxyBeanMethods 特性。
proxyBeanMethods 属性**:Spring Boot 2.x 新增,默认值为true。
点击查看代码
// 无Bean依赖场景,设置proxyBeanMethods=false优化性能
@Configuration(proxyBeanMethods = false)
public class SimpleConfig {
@Bean
public User user() {
return new User();
}
}
核心作用
代理机制:当类被@Configuration注解标记时,Spring会将其转换为CGlib代理对象。代理对象在调用Bean方法时,会优先从容器中查找已存在的Bean实例,避免重复创建,确保单例性。
单例保证:若未加@Configuration,配置类中的Bean方法可能因直接调用导致重复实例化(如示例中@Bean方法内调用其他Bean方法时)。
与普通注解的区别
@Component:仅标记为Bean,但不触发代理,无法保证单例。
@Configuration:隐含@Component,并启用代理机制,是Spring Boot等框架默认使用的配置方式。
使用场景
定义Bean工厂方法(如@Bean注解的方法)时,必须加@Configuration以确保依赖注入正确。
避免在非配置类中使用,否则可能引发单例失效问题。
点击查看代码
@Configuration
public class MiddlewareConfig {
@Bean
public MessageQueue messageQueue() {
// 创建并返回消息队列实例
return new SomeMessageQueueImplementation();
}
}
5.@ConfigurationProperties
将配置文件中的属性绑定到Java类的字段上,常用于Spring Boot项目中管理外部配置。
核心功能
属性绑定:通过指定前缀(prefix),将配置文件(如application.properties)中以该前缀开头的属性自动映射到类的字段上。
与组件注解配合:通常与@Component、@Service或@Configuration一起使用,标记为Spring管理的Bean。
点击查看代码
配置文件(application.properties)
database.url=jdbc:mysql://localhost:3306/mydb
database.username=root
database.password=secret
Java类定义
@Component
@ConfigurationProperties(prefix = "database")
public class DatabaseProperties {
private String url;
private String username;
private String password;
// 需提供getter和setter方法
}

浙公网安备 33010602011771号