想要实现的功能:

我想在配置文件中设置一个开关,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);
    }
}

 

相关文章:

  • 2022-02-04
  • 2022-12-23
  • 2021-07-14
  • 2022-01-16
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-19
  • 2021-06-18
  • 2021-07-24
  • 2021-08-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案