Spring - Multiple Profiles active

后端 未结 4 792
遇见更好的自我
遇见更好的自我 2020-12-24 08:08

I basically have a bean in Spring that I wanted to activate only when 2 profiles are active. Basically, it would be like:

@Profile({\"Tomcat\", \"Linux\"})
p         


        
4条回答
  •  一生所求
    2020-12-24 08:21

    Unfortunately, @Profile activates if any listed profile is active. There are a couple of ways around this.

    • Apply the common @Profile("Tomcat") annotation to a top-level configuration class, and then apply @Profile("Windows") to a nested configuration class (or @Bean method).
    • If Spring Boot is acceptable as a dependency, use @AllNestedConditions to create an annotation that's the AND instead of the OR.

    It looks like what you're trying to do would be clean to write if you were using Spring Boot autoconfiguration classes; if it's practical to introduce autoconfiguration at this stage of your application's lifecycle, I recommend considering it.

提交回复
热议问题