我有下面的 bean 定义。如果我将“exposeSystemProperties”bean 的 placeholderPrefix 更改为“${”并在第二个 bean 的属性路径中使用它,它就会起作用。如果我将其更改为“%{”以外的任何内容,它将不起作用。我不能使用任何其他字符串(例如“$sys{”、“#[”等)。我目前使用的是 3.0.5.RELEASE。
有什么想法吗?更重要的是,我有第三个 PropertyPlaceHolderConfigure,因此只有两个前缀是行不通的。
<bean id="exposeSystemProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="placeholderPrefix"><value>$sys{</value></property>
<property name="order" value="10" />
</bean>
<bean id="localFileProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_NEVER" />
<property name="placeholderPrefix" value="%{" />
<property name="placeholderSuffix" value="}" />
<property name="order" value="20" />
<property name="locations">
<array>
<bean class="java.lang.String">
<constructor-arg><value>classpath:properties/$sys{deploy.env}/client.properties</value></constructor-arg>
</bean>
</array>
</property>
</bean>
最佳答案
由于您需要前缀来控制特定于环境的属性,因此可以通过使用系统变量(而不是示例中的 deploy.env
property 来完成此操作):
<value>classpath:properties/${ENV_SYSTEM:dev}/client.properties</value>
在这种情况下,它总是会在下面查找:
<value>classpath:properties/dev/client.properties</value>
默认情况下,除非设置了ENV_SYSTEM
系统变量。例如,如果设置为“qa”,它将自动查找:
<value>classpath:properties/qa/client.properties</value>
如果您愿意“展望 future ”,另一种方法是使用 Spring 3.1 的 PROFILE feature ,其中 beans 可以是特定于配置文件的。例如:
<beans profile="dev">
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:com/bank/config/sql/schema.sql"/>
<jdbc:script location="classpath:com/bank/config/sql/test-data.sql"/>
</jdbc:embedded-database>
</beans>
仅当配置文件设置为 dev
时才会加载此dataSource
:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles( "dev" );
ctx.load( "classpath:/org/boom/bang/config/xml/*-config.xml" );
ctx.refresh();
关于java - 为什么 Spring 3.x 忽略 PropertyPlaceholderConfigurer 的某些占位符前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7602690/