How to conditionally declare Bean when multiple profiles are not active?

前端 未结 5 1247
猫巷女王i
猫巷女王i 2020-11-30 03:22

In my Spring-Boot-App I want to conditionally declare a Bean, depending on (un)loaded spring-profiles.

The conditon:

Profile \"a\" NOT loaded  
AND           


        
5条回答
  •  抹茶落季
    2020-11-30 04:12

    Unfortunately I don't have a shorter solution for you, but if it is suitable in your case to create the same beans for each profile, you may consider the following approach.

    @Configuration
    public class MyBeanConfiguration {
    
       @Bean
       @Profile("a")
       public MyBean myBeanForA() {/*...*/}
    
       @Bean
       @Profile("b")
       public MyBean myBeanForB() {/*...*/}
    
       @Bean
       @ConditionalOnMissingBean(MyBean.class)
       public MyBean myBeanForOthers() {/*...*/}
    
    }
    

提交回复
热议问题