Java 读取Spring中jar中的配置文件

Java 读取Spring中jar中的配置文件,java,spring,Java,Spring,你好 我有一个spring应用程序,它依赖于传统的spring应用程序,作为一个jar提供。请注意,遗留jar在jar内部有它的spring配置文件。有两个属性文件:app.properties和override.properties 现在,从外部项目中,我可以使用以下方法读取一个配置属性: <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <prope

你好

我有一个spring应用程序,它依赖于传统的spring应用程序,作为一个jar提供。请注意,遗留jar在jar内部有它的spring配置文件。有两个属性文件:app.properties和override.properties

现在,从外部项目中,我可以使用以下方法读取一个配置属性:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" ref="propertyResource"></property>
 </bean>

<bean name="propertyResource" class="org.springframework.core.io.ClassPathResource">
  <constructor-arg><value>spring-config.properties</value></constructor-arg>
 </bean> 
但我无法使它适用于2个属性文件。是否有人遇到过类似的问题并找到了解决办法?请建议。我尝试使用list for PropertyResourcebean,有两个PropertyPlaceholderConfigurer bean,但没有任何用处


顺便说一句,只是为了记录,我已经搜索了spring文档,但还没有彻底搜索,所以这将是我下一步要做的事情,但是如果有人已经知道了解决方案,为什么要重新发明轮子呢。

您所需要的只是ignore Unsolvable placeholders属性:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" ref="propertyResource"></property>
  <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
几点注意:

请看一下注意事项:此链接很旧,但仍与速记配置相关。您的配置将变成:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
        p:ignoreUnresolvablePlaceholders="true" 
        p:locations="classpath:spring-config.properties" />
<context:property-placeholder locations="classpath:spring-config.properties" ignore-unresolvable=true"/>
另请参阅第c.2.8节。您的配置将变成:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
        p:ignoreUnresolvablePlaceholders="true" 
        p:locations="classpath:spring-config.properties" />
<context:property-placeholder locations="classpath:spring-config.properties" ignore-unresolvable=true"/>

您只需要“忽略不可解析的占位符”属性:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location" ref="propertyResource"></property>
  <property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
几点注意:

请看一下注意事项:此链接很旧,但仍与速记配置相关。您的配置将变成:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
        p:ignoreUnresolvablePlaceholders="true" 
        p:locations="classpath:spring-config.properties" />
<context:property-placeholder locations="classpath:spring-config.properties" ignore-unresolvable=true"/>
另请参阅第c.2.8节。您的配置将变成:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
        p:ignoreUnresolvablePlaceholders="true" 
        p:locations="classpath:spring-config.properties" />
<context:property-placeholder locations="classpath:spring-config.properties" ignore-unresolvable=true"/>

如果我理解正确,您正在尝试将多个属性文件加载到PropertyPlaceHolderConfigure中。您可以通过设置“locations”属性而不是“location”属性来完成此操作

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

如果我理解正确,您正在尝试将多个属性文件加载到PropertyPlaceHolderConfigure中。您可以通过设置“locations”属性而不是“location”属性来完成此操作

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

我试过这个。不知何故,我无法使用此方法加载jar中存在的属性文件。我也尝试过使用classpath*。你能确认jar中属性文件的路径位置吗?你收到错误信息了吗?谢谢凯文的回复。这些文件位于jar文件的根目录下。我将尝试重建jar并在此处更新。您是否收到任何错误消息?你能描述一下你说你不能让它工作是什么意思吗。你看到的是什么行为?凯文,发现罐子有问题。重建jar之后,使用您的解决方案,我能够从jar中加载配置文件。谢谢我试过这个。不知何故,我无法使用此方法加载jar中存在的属性文件。我也尝试过使用classpath*。你能确认jar中属性文件的路径位置吗?你收到错误信息了吗?谢谢凯文的回复。这些文件位于jar文件的根目录下。我将尝试重建jar并在此处更新。您是否收到任何错误消息?你能描述一下你说你不能让它工作是什么意思吗。你看到的是什么行为?凯文,发现罐子有问题。重建jar之后,使用您的解决方案,我能够从jar中加载配置文件。谢谢