Java主类在运行时重写属性文件
我正在使用ApacheMaven和Spring。在src/main/resources文件夹中,我有一个属性文件。这些特性值可以具有不同的值 我正在使用PropertyPlaceHolderConfigureJava主类在运行时重写属性文件,java,spring,maven,properties,jar,Java,Spring,Maven,Properties,Jar,我正在使用ApacheMaven和Spring。在src/main/resources文件夹中,我有一个属性文件。这些特性值可以具有不同的值 我正在使用PropertyPlaceHolderConfigure @Configuration public class ResourceConfig { @Bean public PropertyPlaceholderConfigurer properties( ) { PropertyPlaceholderConfigurer ppc =
@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
}
ApacheMaven构建应用程序时,属性文件将位于类路径上。在单元测试期间使用属性文件。我想知道在主程序开始生产之前,如何用新的属性文件替换属性
我已经看到了Properties.load()的一些示例,但我不想这样做。我想通过被替换的主程序接受一个属性文件,因此Spring端启动PropertyPlaceHolderConfigure
@Configuration
public class ResourceConfig {
@Bean
public PropertyPlaceholderConfigurer properties( ) {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
ppc.setLocations(new ClassPathResource[] {new ClassPathResource("propertiesFile")});
return ppc;
}
}
如何实现这一点?您可以将测试属性文件放在
src/test/resources/
中。在测试类中,它将使用此位置的属性文件
放置在该位置上方的属性文件将不会包含在最终生成的类路径中
使用
src/main/resources
将您想要的资源文件放置在我理解的主程序中,但其思想是在启动Java时在命令提示符下接受属性文件。我不想将生产属性文件放在src/main/resources中。实际上可以这样做吗?