gpt4 book ai didi

java - 将属性文件添加到预先存在的 PropertyPlaceholderConfigurer

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:24:20 25 4
gpt4 key购买 nike

我有一个项目,我分为几个子项目,所以这就是层次结构

parent
-project A
-project B

现在在项目 A 中,我这样定义我的属性文件:

  <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="myProperties" />
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
</bean>
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:file1.properties</value>
<value>classpath:file2.properties</value>
<value>classpath:file3.properties</value>
</list>
</property>
</bean>

现在我的项目B也需要一个property文件,感觉这个properties文件属于B子项目。我如何才能将此属性文件“合并”到 propertyPlaceHolderConfigurer bean 中,而不替换之前从项目 A 加载的属性文件?

最佳答案

将子模块的属性文件放在 src/main/resources/META-INF 中,这样您就可以按如下方式从父模块加载它们:

<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="locations">
<list>
<value>classpath*:parent1.properties</
<value>classpath*:parent2.properties</value>

<!-- loads all submodules property-files --!>
<value>classpath*:/META-INF/*.properties</value>
</list>
</property>
</bean>

来自 Spring documentation :

The " classpath*:" prefix can also be combined with a PathMatcher pattern in the rest of the location path, for example " classpath*:META-INF/*-beans.xml". [...]

Please note that " classpath*:" when combined with Ant-style patterns will only work reliably with at least one root directory before the pattern starts, unless the actual target files reside in the file system. This means that a pattern like " classpath*:*.xml" will not retrieve files from the root of jar files but rather only from the root of expanded directories. [...]

关于java - 将属性文件添加到预先存在的 PropertyPlaceholderConfigurer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24167025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com