Java 在我的 applicationContext 中是否可以有多个 PropertyPlaceHolderConfigurer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/686635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:56:00  来源:igfitidea点击:

Is it possible to have multiple PropertyPlaceHolderConfigurer in my applicationContext?

javaspring

提问by Mauli

I need to load a specific applicationContext.xml file according to a given system property. This itself loads a file with the actual configuration. Therefore I need 2 PropertyPlaceHolderConfigurer, one which resolves the system param, and the other one within the actual configuration.

我需要根据给定的系统属性加载特定的 applicationContext.xml 文件。这本身会加载一个包含实际配置的文件。因此我需要 2 个 PropertyPlaceHolderConfigurer,一个解决系统参数,另一个在实际配置中。

Any ideas how to do this?

任何想法如何做到这一点?

采纳答案by flicken

Yes you can do more than one. Be sure to set ignoreUnresolvablePlaceholdersso that the first will ignore any placeholders that it can't resolve.

是的,你可以做不止一个。确保设置ignoreUnresolvablePlaceholders以便第一个将忽略它无法解析的任何占位符。

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

<bean id="ppConfig2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="ignoreUnresolvablePlaceholders" value="false"/>
   <property name="locations">
    <list>
             <value>classpath*:/myOther.properties</value>
    </list>
  </property>
</bean>

Depending on your application, you should investigate systemPropertiesMode, it allows you to load properties from a file, but allow the system properties to override values in the property file if set.

根据您的应用程序,您应该调查 systemPropertiesMode,它允许您从文件加载属性,但允许系统属性覆盖属性文件中的值(如果已设置)。

回答by Trenton

Beware -- there might be a bug related to multiple configurers. See http://jira.spring.io/browse/SPR-5719for more details.

当心 - 可能存在与多个配置器相关的错误。有关更多详细信息,请参阅http://jira.spring.io/browse/SPR-5719

I'm unable to get multiple to work locally... but I'm not yet blaming anyone but myself.

我无法让多个人在本地工作......但我还没有责怪除了我自己之外的任何人。

回答by weekens

Another solution is to use placeholderPrefix property of PropertyPlaceholderConfigurer. You specify it for the second (third, fourth...) configurer, and then prefix all your corresponding placeholders, thus there will be no conflict.

另一种解决方案是使用 PropertyPlaceholderConfigurer 的 placeholderPrefix 属性。您为第二个(第三个,第四个...)配置器指定它,然后为所有相应的占位符添加前缀,因此不会发生冲突。

<bean id="mySecondConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="classpath:/myprops.properties" 
        p:placeholderPrefix="myprefix-"/>

<bean class="com.mycompany.MyClass" p:myprop="${myprefix-value.from.myprops}"/>

回答by user1071914

You can't do this directly, and this JIRA issue from Spring explains why (check the comment from Chris Beams for a detailed explanation):

您不能直接执行此操作,Spring 的这个 JIRA 问题解释了原因(查看 Chris Beams 的评论以获得详细解释):

https://jira.springsource.org/browse/SPR-6428

https://jira.springsource.org/browse/SPR-6428

However, he does provide a workaround using Spring 3.1 or later, which is to use the PropertySourcesPlaceholderConfigurer class instead of PropertyPlaceholderConfigurer class.

但是,他确实提供了使用 Spring 3.1 或更高版本的解决方法,即使用 PropertySourcesPlaceholderConfigurer 类而不是 PropertyPlaceholderConfigurer 类。

You can download a Maven-based project that demonstrates the problem and the solution from the Spring framework issues github:

您可以从 Spring 框架问题 github 下载一个基于 Maven 的项目,该项目演示了该问题和解决方案:

https://github.com/SpringSource/spring-framework-issues

https://github.com/SpringSource/spring-framework-issues

Look for the issue number, SPR-6428, in the downloaded projects.

在下载的项目中查找问题编号 SPR-6428。

回答by Vadzim

We have the following approach working:

我们有以下方法工作:

<util:properties id="defaultProperties">
    <prop key="stand.name">DEV</prop>
    <prop key="host">localhost</prop>
</util:properties>
<context:property-placeholder 
    location="file:${app.properties.path:app.properties}" 
    properties-ref="defaultProperties"/>

System property app.properties.pathcan be used to override path to config file.

系统属性app.properties.path可用于覆盖配置文件的路径。

And application bundles some default values for placeholders that cannot be defined with defaults in common modules.

并且应用程序为占位符捆绑了一些默认值,这些默认值无法在通用模块中使用默认值进行定义。

回答by SylvainR

On my own side, playing with PropertyPlaceholderConfigurer both properties :

就我自己而言,使用 PropertyPlaceholderConfigurer 这两个属性:

  • order (should be lower for first accessed/parsed PPC)
  • ignoreUnresolvablePlaceholders ("false" for first accessed/parsed PPC, "true" for next one)

  • and also give 2 distinct id(s) to both PPC (to avoid one to be overwritten by the other)

  • 顺序(第一次访问/解析的 PPC 应该更低)
  • ignoreUnresolvablePlaceholders(第一次访问/解析的PPC为“false”,下一个为“true”)

  • 并为两个 PPC 提供 2 个不同的 id(以避免一个被另一个覆盖)

works perfectly

完美运行

Hope it helps

希望能帮助到你

回答by s91g

Just giving 2 distinct ids worked for me. I am using spring 3.0.4.

只提供 2 个不同的 ID 对我有用。我正在使用 spring 3.0.4。

Hope that helps.

希望有帮助。

回答by Java_deep

In case, you need to define two PPC's (like in my situation) and use them independently. By setting property placeholderPrefix, you can retrieve values from desired PPC. This will be handy when both set of PPC's properties has same keys, and if you don't use this the property of ppc2 will override ppc1.

以防万一,您需要定义两个 PPC(如我的情况)并独立使用它们。通过设置属性placeholderPrefix,您可以从所需的 PPC 中检索值。当两组 PPC 的属性具有相同的键时,这将很方便,如果您不使用它,ppc2 的属性将覆盖 ppc1。

Defining your xml:

定义您的 xml:

<bean name="ppc1"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="ref to your props1" />
        <property name="placeholderPrefix" value="$prefix1-{" />
    </bean>
<bean name="ppc2"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="ref to your props2" />
        <property name="placeholderPrefix" value="$prefix2-{" />
    </bean>

Retrieving during Run time:

在运行时检索:

@Value(value = "$prefix1-{name}")
private String myPropValue1;

@Value(value = "$prefix2-{name}")
private String myPropValue2;