【发布时间】:2011-02-16 21:15:14
【问题描述】:
我的问题如下:
我有 server.properties 用于不同的环境。这些属性的路径是通过名为propertyPath 的系统属性提供的。我如何指示我的applicationContext.xml 加载具有给定propertyPath 系统属性的属性,而没有一些丑陋的MethodInvokingBean 调用System.getProperty('');
我的 applicationContext.xml
<bean id="systemPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="placeholderPrefix" value="sys{"/>
<property name="properties">
<props>
<prop key="propertyPath">/default/path/to/server.properties</prop>
</props>
</property>
</bean>
<bean id="propertyResource" class="org.springframework.core.io.FileSystemResource" dependency-check="all" depends-on="systemPropertyConfigurer">
<constructor-arg value="sys{propertyPath}"/>
</bean>
<bean id="serviceProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" ref="propertyResource"/>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" ref="propertyResource"/>
<property name="placeholderPrefix" value="prop{"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="ignoreResourceNotFound" value="false"/>
</bean>
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="prop{datasource.name}"/>
</bean>
使用这种配置,propertyResource 总是抱怨
java.io.FileNotFoundException: sys{propertyPath} (The system cannot find the file specified)
有什么建议吗? ;-) 谢谢加布
编辑:
现在我调试了 bean 的加载过程,似乎在创建 systemPropertyConfigurer 之前调用了 propertyConfigurer 的 setLocation 方法,因此 propertyResource 用“sys{propertyPath}”初始化。
我玩过depends-on,但没有运气。
【问题讨论】:
-
你是怎么玩depends-on的?
-
'propertyResource depends-on="systemPropertyConfigurer"' 似乎没有效果。 propertyResource 首先被初始化,可能是因为它是用 constructor-arg 初始化的
标签: spring properties