我有 Spring Framework 的下一个属性文件
配置.属性
有内容
environment=devel //posible values: devel, testing, prod
并使用之前的环境属性,选择以下一些文件进行动态加载
config-service1-devel.properties
config-service1-testing.properties
config-service1-prod.properties
config-serviceN-devel.properties
config-serviceN-testing.properties
config-serviceN-prod.properties
然后,使用 spring 我想加载属性,我解决了加载第一个属性文件的问题,但我不明白如何使用表达式语言来完成依赖属性的值。
<bean id="MainApplicationProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"
value="file://#{systemProperties['jboss.server.home.dir']}/conf/services.properties" />
<property name="placeholderPrefix" value="$mainProperty{" />
<property name="placeholderSuffix" value="}" />
</bean>
<bean id="SecondApplicationProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
depends-on="MainApplicationProperties">
<property name="locations">
<list>
<value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceOne/service1-$mainProperty{environment}.properties</value>
<value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceTwo/service2-$mainProperty{environment}.properties</value>
<value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceN/serviceN-$mainProperty{environment}.properties</value>
</list>
</property>
</bean>
错误输出是下一个,
java.io.FileNotFoundException: /..../conf/serviceOne/service1-$mainProperty{environment}.properties (No such file or directory)
我的观点是,值(value)没有被取代
帮帮我,谢谢
最佳答案
问题是当BeanFactoryPostProcessors
开始被调用时,它们已经被实例化了。因此,即使您第一个 PropertyPlaceholderConfigurer
修改了第二个 PropertyPlaceholderConfigurer
的 bean 定义,它也没有任何效果,因为两个 bean 都已实例化。
关于java - Spring-如何使用PropertyPlaceholderConfigurer动态加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16993311/