@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 注釋的類上使用


免責聲明!

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



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