繁体   English   中英

在Spring中读取jar中存在的配置文件

[英]Reading config files present inside the jar in Spring

问候!

我有一个依赖于旧版Spring应用程序的spring应用程序,以jar形式提供。 请注意,旧版jar本身具有其spring config文件。 好,有两个属性文件: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个属性文件。 是否有人遇到类似问题并找到了出路? 请建议。 我尝试对propertyResource bean使用list,有两个PropertyPlaceholderConfigurer bean,但是没有用。

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

如果我对您的理解正确,那么您正在尝试将多个属性文件加载到PropertyPlaceholderConfigurer中。 您可以通过设置'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>

您需要做的是忽略不可解析的占位符属性:

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

一些注意事项:

看一下速记配置的p名称空间 (注意:此链接很旧,但仍然有用)。 您的配置将变为:

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

还要看看上下文名称空间 (c.2.8节)。 您的配置将变为:

<context:property-placeholder locations="classpath:spring-config.properties" ignore-unresolvable=true"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM