在我的应用程序中,我使用@Profile("prod")和注释bean @Profile("demo").第一个,正如你可以猜到:),用于连接到生产数据库的bean,第二个用于注释使用一些假数据库(HashMap或其他)的bean - 以加快开发速度.
我想要的是默认配置文件("prod"),如果它没有被" something-else " 覆盖,它将被使用.
完美将是我的web.xml:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
然后覆盖它,-Dspring.profiles.active="demo"以便我可以这样做:
mvn jetty:run -Dspring.profiles.active="demo".
Run Code Online (Sandbox Code Playgroud)
但遗憾的是,这不起作用.任何想法我怎么能得到那个?-Dspring.profiles.active="prod"不能在我的所有环境中进行设置.
在我的Spring-Boot-App中,我想有条件地声明一个Bean,这取决于(un)加载的spring-profiles.
条件:
Profile "a" NOT loaded
AND
Profile "b" NOT loaded
Run Code Online (Sandbox Code Playgroud)
到目前为止我的解决方案(有效):
@Bean
@ConditionalOnExpression("#{!environment.getProperty('spring.profiles.active').contains('a') && !environment.getProperty('spring.profiles.active').contains('b')}")
public MyBean myBean(){/*...*/}
Run Code Online (Sandbox Code Playgroud)
是否有更优雅(和更短)的方式来解释这种情况?
特别是我想在这里摆脱Spring Expression Language的使用.