设置物业位置的环境

时间:2017-08-29 09:00:25

标签: java spring

我正在使用属性文件配置我的spring应用程序。但我必须在开发和生产属性文件之间切换。目前我有这个代码片段

@Configuration
@PropertySource(value = "classpath:config/simulator.properties", ignoreResourceNotFound = false)
public class AppConfiguration

但是我想要一些值=“classpath:$ {env:local} /simulator.properties”

这意味着如果我没有设置环境变量env,那么它必须指向local / simulator.properties else,如果环境env变量指向生产,则该位置必须是production / simulator.properties。

因此,本地是后备环境。

有没有办法实现这一目标。我不想使用配置文件,它必须由环境变量

控制

我不想为个人资料

设置-D选项

由于

约翰

3 个答案:

答案 0 :(得分:1)

您可以使用多个@PropertySource注释,如果找到第一个文件和第二个文件,并且两个文件中的键都匹配,则将采用后一个。请查看here

@PropertySource(value="classpath:local/simulator.properties",ignoreResourceNotFound=true)
@PropertySource(value="classpath:${env.production}/simulator.properties",ignoreResourceNotFound=true)

答案 1 :(得分:0)

如果我们有窗口,那么Spring会自动从系统根路径查看c:/将被spring自动理解,如果我们有Linux机器那么/将是root。 所以这里我们不需要设置classpath或-D标签。

答案 2 :(得分:0)

实际上在我的测试中(单元测试4)我使用了注释:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:com/myTest/MyTestApplicationContext.xml"})

这允许您为每个测试提供单独的应用程序上下文,并为开发环境单独设置一个。当然,在每个应用程序上下文中,您都可以配置要从不同位置读取的属性。对我很有用

相关问题