@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(){

}
原文地址:https://www.cnblogs.com/myf008/p/11003879.html