我正在使用 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 时,它无法加载属性。
最佳答案
我首先加载父属性文件,然后在系统环境变量中设置特定的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" />
关于java - 如何使用 Spring PropertyPlaceholderConfigurer 从另一个属性文件调用属性文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9591701/