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

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM