我正在使用Spring开发一个命令行Java应用程序。我有多个属性文件存储在不同的位置,一个属性文件包含所有这些属性的路径。我正在使用PropertyPlaceholderConfigurer来读取包含不同属性文件位置的属性。我不确定处理多个属性的最佳方法。
应用程序的工作原理如下:我将使用JVM命令-Dmypath = parent.properties传递第一个属性文件的路径。属性文件如下所示:
child1=/location1/child1.properties
child2=/location2/child2.properties
等等
我的父属性配置如下所示:
<bean id="parentProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>${mypath}</value>
</list>
</property>
</bean>
child1配置看起来:
<bean id="child1Property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>${child1}</value>
</list>
</property>
</bean>
现在当我调用child1时,它无法加载属性。
答案 0 :(得分:1)
我首先加载父属性文件,然后在系统环境变量中设置特定的child1和child2变量,并从系统环境变量加载。并且工作正常。
代码:
<context:property-placeholder location="file:${mypath}/*.properties" ignore-unresolvable="true" />
<bean id="systemPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" value="#{@systemProperties}" />
<property name="targetMethod" value="putAll" />
<property name="arguments">
<!-- The new Properties -->
<util:properties>
<prop key="LOG_LOCATION">${log.location}</prop>
<prop key="child1">${child1}</prop>
</util:properties>
</property>
</bean>
<context:property-placeholder location="file:#{systemProperties['child1']}/*.sql" ignore-unresolvable="true" />
答案 1 :(得分:0)
可以通过设置属性BeanFactoryPostProcessors
来设置PropertyPlaceholderConfigurer
的执行顺序,例如"order"
(请参阅Ordered)。通过将parentProperty
的执行优先级设置为高于child1Property
的执行优先级,您可以确保parentProperty
先运行,并配置${child1}
的值。
答案 2 :(得分:0)
从类路径加载属性可能更容易,其中位置包含在类路径中,而不是在文件中,然后以下内容将加载所有属性文件。
<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*:*.properties</value>
</list>
</property>
</bean>