@ConditionalOnMissingBean

该注解表示,如果存在它修饰的类的bean,则不需要再创建这个bean;

 

可以给该注解传入参数例如@ConditionOnMissingBean(name = "example"),

这个表示如果name为“example”的bean存在,这该注解修饰的代码块不执行。

 

    @Bean
    @ConditionalOnMissingBean(name = "imageValidateCodeGenerator")
    public ValidateCodeGenerator imageValidateCodeGenerator() {
        ImageCodeGenerator codeGenerator = new ImageCodeGenerator();
        codeGenerator.setSecurityProperty(securityProperties);
        return codeGenerator;
    }

 

测试发现,@ConditionOnMissingBean 只能在@Bean 注释的方法上使用,不能再@Component 注释的类上使用

posted on 2018-02-21 20:35  HB1  阅读(13910)  评论(0)    收藏  举报

导航