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