Spring-boot中使用@ConditionalOnExpression注解,在特定情況下初始化bean


想要實現的功能:

我想在配置文件中設置一個開關,enabled,在開關為true的時候才實例化bean,進行相關業務邏輯的操作。

具體實現:

1:要實例化的bean

2. 配置類

 

代碼:

想要實例化的bean:在這個類上不要加@Component注解

public class OrderMessageMonitor {

    public OrderMessageMonitor(ConfigContext configContext) {
    ……
    
    }
    public doSomeThing() {
    }

}

配置類:

@Configuration
@ConditionalOnExpression("${enabled:false}")
public class BigpipeConfiguration {
    @Bean
    public OrderMessageMonitor orderMessageMonitor(ConfigContext configContext) {
        return new OrderMessageMonitor(configContext);
    }
}

 


免責聲明!

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



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