【发布时间】:2014-10-28 23:07:30
【问题描述】:
我正在使用 Apache Maven 和 Spring。在 src/main/resources 文件夹中,我有一个属性文件。这些属性值可以有不同的值。
我正在使用 PropertyPlaceholderConfigurer。
@Configuration
public class ResourceConfig {
@Bean
public PropertyPlaceholderConfigurer properties( ) {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
ppc.setLocations(new ClassPathResource[] {new ClassPathResource("propertiesFile")});
return ppc;
}
}
我在运行时替换这些值:
@Configuration
public class DataSourceConfig {
@Value("${jdbc.url}")
private String jdbcUrlDefault;
}
这只是一个示例。我有一个主要方法:
public static void main(String[] args) {
// accept a properties file and replace those values defined in DataSourceConfig class
}
当 Apache Maven 构建应用程序时,属性文件将位于类路径中。在单元测试期间使用属性文件。我想了解在主程序投入生产之前如何用新的属性文件替换属性。
我见过一些Properties.load() 的例子,但我不想这样做。我想通过被替换的主程序接受一个属性文件,所以Spring端启动PropertyPlaceholderConfigurer。
如何做到这一点?
【问题讨论】:
标签: java spring maven properties jar