@Conditional系列注解例子
1. @Conditional
说明:指定的Condition实现类,matches方法返回true则注入bean,false则不注入
@Configuration public class BeanConfig { //只有一个类时,大括号可以省略 //如果WindowsCondition的实现方法返回true,则注入这个bean @Conditional({WindowsCondition.class}) @Bean(name = "bill") public Person person1(){ return new Person("Bill Gates",62); } //如果LinuxCondition的实现方法返回true,则注入这个bean @Conditional({LinuxCondition.class}) @Bean("linus") public Person person2(){ return new Person("Linus",48); } }
创建 LinuxCondition和 WindowsCondition类,并实现Condition接口
public class LinuxCondition implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { Environment environment = conditionContext.getEnvironment(); String property = environment.getProperty("os.name"); if (property.contains("Linux")){ return true; } return false; } }
2. @ConditionalOnBean
说明:仅仅在当前上下文中存在某个对象时,才会实例化一个Bean
//RedisOperBean依赖redisTemplate @Component @ConditionalOnBean(name="redisTemplate") public class RedisOperBean { private final RedisTemplate redisTemplate; public RedisOperBean(RedisTemplate redisTemplate) { // ... } }
3. @ConditionalOnClass
说明:某个class位于类路径上,才会实例化一个Bean,要求指定的class必须存在
4. @ConditionalOnProperty
说明:
(1)matchIfMissing:从application.properties中读取某个属性值,如果该值为空,默认值为true
(2)havingValue:通过其两个属性name以及havingValue来实现的,其中name用来从application.properties中读取某个属性值,如果该值为空,则返回false;
如果值不为空,则将该值与havingValue指定的值进行比较,如果一样则返回true;否则返回false。如果返回值为false,则该configuration不生效;为true则生效。
@Configuration @ConditionalOnClass({ Feign.class }) @ConditionalOnProperty(value = "feign.oauth2.enabled", havingValue = "true", matchIfMissing = true) public class OAuth2FeignAutoConfiguration { @Bean @ConditionalOnBean(OAuth2ClientContext.class) public RequestInterceptor oauth2FeignRequestInterceptor(OAuth2ClientContext oauth2ClientContext) { return new OAuth2FeignRequestInterceptor(oauth2ClientContext); } }
5.@ConditionalOnMissingBean
说明:仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean
@Bean @ConditionalOnMissingBean(name = "imageValidateCodeGenerator") public ValidateCodeGenerator imageValidateCodeGenerator() { ImageCodeGenerator codeGenerator = new ImageCodeGenerator(); codeGenerator.setSecurityProperty(securityProperties); return codeGenerator; }
6.@ConditionalOnMissingClass
说明:某个class类路径上不存在的时候,才会实例化一个Bean
7.@ConditionalOnExpression
说明:当表达式为true的时候,才会实例化一个Bean
@Configuration @ConditionalOnExpression("${enabled:false}") public class BigpipeConfiguration { @Bean public OrderMessageMonitor orderMessageMonitor(ConfigContext configContext) { return new OrderMessageMonitor(configContext); } }
其他样式:
@ConditionalOnExpression("${mq.cumsumer.enabled}==1&&${rabbitmq.comsumer.enabled:true}")
@ConditionalOnExpression("'${mq.comsumer}'.equals('rabbitmq')")
8.@ConditionalOnNotWebApplication
说明:当前不是web应用
9.@ConditionalOnWebApplication
说明:当前是web应用
10. @ConditionalOnResource
说明:类路径下是否存在指定的资源文件
@Bean @ConditionalOnResource(resources="classpath:shiro.ini") protected Realm iniClasspathRealm(){ }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异