java - 外部化属性工作但不覆盖内部属性中的现有默认值

标签 java spring maven tomcat

我有一个多模块 maven 项目,每个模块都是一个 jar,具有自己的上下文文件和一组捆绑在 JAR 中的默认属性。

基本上在核心模块中我有这个:

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:core.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

现在我有了另一个模块,它通过加载所有相关的 XML 并添加自己的配置文件来构建应用程序

<import resource="classpath*:core-context.xml" />


<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>#{systemEnvironment['foo_bar']}</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

发生的事情是我的所有配置文件都工作正常,除了具有默认值的那个。我无法在 application.properties 或系统变量指向的文件中覆盖它们。

我尝试使用 spring.config.location 强制配置文件。我确实在 tomcat 中看到消息说我有配置 -Dspring.config.file=file:///... (windows 路径)。但是 spring 完全忽略了它(Spring 的日志中没有消息)。

我尝试切换到 PropertySources 类,但没有成功。

我非常希望不必删除所有默认属性并将所有内容放在外部文件中,因为很多参数都是应用程序内部的,对客户端没有任何值(value)。

那么我在这里缺少什么?

最佳答案

好吧,经过几个小时后,我终于发现了一些非常……简单的事情。 技巧如下:在导入之前声明属性占位符。

当您在属性占位符中声明文件时,它是最后一个获胜,这里似乎恰恰相反:第一个占位符获胜。

    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>#{systemEnvironment['foo_bar']}</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>

 <import resource="classpath*:core-context.xml" />

关于java - 外部化属性工作但不覆盖内部属性中的现有默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41977892/

相关文章:

java - 这是什么意思 "couldn' t read CGI input from STDIN after alloc read 0 "

java - "variable is accessed from within inner class needs to be declared final"错误

java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Unknown database 'xyz' . hbm2ddl.auto=更新不工作

spring - @Configuration 和 @Component 之间的行为差​​异

java - 如何在 Java/Android 中解析 SVG?

java - JPA - 将对象级联保存到由 id 映射的数据库

java - 从 Spring Controller 强制下载 CSV

java - 自定义 Maven 插件和库

scala - Intellij 多模块 maven 项目,其中一个子模块中的更新不会传播到 war

java - Maven 找不到从另一个模块导入的类,但 Intellij 找到了