想要實現的功能:
我想在配置文件中設置一個開關,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); } }
