Java 如何使用Spring PropertyPlaceHolderConfigure从另一个属性文件调用属性文件?
我正在使用Spring开发一个命令行Java应用程序。我有多个存储在不同位置的属性文件和一个包含所有这些属性路径的属性文件。我正在使用PropertyPlaceHolderConfigure来读取包含不同属性文件位置的属性。我不确定处理多个属性的最佳方式 应用程序的工作方式如下:我将使用JVM命令-Dmypath=parent.properties传递第一个属性文件的路径。属性文件将如下所示:Java 如何使用Spring PropertyPlaceHolderConfigure从另一个属性文件调用属性文件?,java,spring,properties,Java,Spring,Properties,我正在使用Spring开发一个命令行Java应用程序。我有多个存储在不同位置的属性文件和一个包含所有这些属性路径的属性文件。我正在使用PropertyPlaceHolderConfigure来读取包含不同属性文件位置的属性。我不确定处理多个属性的最佳方式 应用程序的工作方式如下:我将使用JVM命令-Dmypath=parent.properties传递第一个属性文件的路径。属性文件将如下所示: child1=/location1/child1.properties child2=/locati
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=/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>
${mypath}
child1配置看起来:
<bean id="child1Property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>${child1}</value>
</list>
</property>
</bean>
${child1}
现在,当我调用child1时,它无法加载属性。可以通过设置属性
“order”
来设置BeanFactoryPostProcessors
的执行顺序,例如PropertyPlaceHolderConfigure
(请参阅)。通过将parentProperty
的执行优先级设置为高于child1Property
的优先级,可以确保parentProperty
首先运行,并配置${child1}的值
可以通过设置属性“order”
来设置BeanFactory后处理器的执行顺序,例如PropertyPlaceHolderConfigure
(请参阅)。通过将parentProperty
的执行优先级设置为高于child1Property
的优先级,您可以确保parentProperty
首先运行,配置${child1}
的值,从类路径加载属性可能会更容易,其中的位置包含在类路径中,而不是在文件中,然后以下内容将加载所有属性文件
<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>
类路径*:*.properties
从类路径加载属性可能会更容易,因为位置包含在类路径中,而不是文件中,然后下面的操作将加载所有属性文件
<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>
类路径*:*.properties
我已先加载父属性文件,然后在系统环境变量中设置特定的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" />
${log.location}
${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" />
${log.location}
${child1}
我尝试在parentProperty中设置order=“1”参数,也尝试添加dependens depen=“parentProperty”,但仍然失败${child1}再次查看您的配置需要解析占位符${mypath}才能加载parentProperty。您需要使用另一个PropertyPlaceHolderConfigure来解析此占位符(将systemsPropertiesMode设置为使用系统属性),或者以另一种方式提供此属性文件的路径。当我运行应用程序时。My parentProperty能够从${myfile}中指定的文件中读取所有属性。我能够读取文件位置以外的属性。但我正在尝试读取包含文件位置的属性,它无法加载文件。它表示资源${child1}的FileNotFoundException我已尝试在parentProperty中设置order=“1”参数,也尝试添加dependens=“parentProperty”,但仍然失败${child1}再次查看您的配置占位符${mypath}需要解析才能加载parentProperty。您需要使用另一个PropertyPlaceHolderConfigure来解析此占位符(将systemsPropertiesMode设置为使用系统属性),或者以另一种方式提供此属性文件的路径。当我运行应用程序时。My parentProperty能够从${myfile}中指定的文件中读取所有属性。我能够读取文件位置以外的属性。但我正在尝试读取包含文件位置的属性,它无法加载文件。它说FileNotFoundException for resource${child1}我的应用程序要求“不要在类路径中放置属性文件”。我的应用程序要求“不要在类路径中放置属性文件”。