Conditional spring bean creation

后端 未结 4 1317
悲哀的现实
悲哀的现实 2020-12-03 04:42

I have a question about Spring annotation configurations. I have a bean:

@Bean 
public ObservationWebSocketClient observationWebSocketClient(){
    log.info(&         


        
4条回答
  •  自闭症患者
    2020-12-03 04:56

    For Spring Boot 2+ you can simply use:

    @Profile("prod")
    or
    @Profile({"prod","stg"})
    

    That will allow you to filter the desired profile/profiles, for production or staging and for the underlying Bean using that annotation it only will be loaded by Springboot when you set the variable spring.profiles.active is equals to "prod" and ("prod" or "stg"). That variable can be set on O.S. environment variables or using command line, such as -Dspring.profiles.active=prod.

提交回复
热议问题