0
我在Spring, Java, Ant
Web应用程序中工作。我正在使用Spring分析来基于环境加载属性。下面是示例使用Spring配置文件加载环境属性
@Profile("dev")
@Component
@PropertySource("classpath:dev.properties")
public class DevPropertiesConfig{
}
@Profile("qa")
@Component
@PropertySource("classpath:qa.properties")
public class TestPropertiesConfig {
}
@Profile("live")
@Component
@PropertySource("classpath:live.properties")
public class LivePropertiesConfig{
}
在web.xml中,我们可以给轮廓
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
现在,我的查询是每一个环境,我需要创建一个单独的Java类。
问:是否有可能只有一个类如提供配置文件名像@Profile({profile})
一些有约束力的参数。
另外,让我知道是否有其他更好的选择可用来实现相同。
是的,这是可能的,你做对了。 –
而不是使用基于活动配置文件的'ApplicationContextInitializer'添加指向该配置文件文件的'ResourcePropertySource'。这也是Spring Boot(或多或少)的作用。 –
有关@ M.Deinum答案的详细示例,请参阅此[SO答案](http://stackoverflow.com/a/8601353/3898076)。 –