【问题标题】:Java Main class overriding properties file at runtimeJava Main 类在运行时覆盖属性文件
【发布时间】: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


    【解决方案1】:

    您可以将测试属性文件放在src/test/resources/ 中。在测试类中,它将使用此位置的属性文件。

    放置在此位置上方的属性文件不会包含在最终构建的类路径中。

    使用src/main/resources在主程序中放置你想要的资源文件

    【讨论】:

    • 我理解,但想法是在启动 Java 时在命令提示符下接受属性文件。我不想将生产属性文件放在 src/main/resources 中。
    • 真的可以吗?
    猜你喜欢
    • 2017-10-14
    • 1970-01-01
    • 2016-01-16
    • 2013-09-07
    • 2013-08-16
    • 2011-04-17
    • 1970-01-01
    • 2012-02-08
    相关资源
    最近更新 更多