Java 为什么Spring3.x会忽略PropertyPlaceHolderConfigure的某些占位符前缀?
我有下面的bean定义。如果我将“exposeSystemProperties”bean的占位符前缀更改为“${”,并在第二个bean的属性路径中使用该前缀,则该前缀有效。如果我将其更改为“%{”以外的任何内容,则该前缀无效。我不能使用任何其他字符串(例如“$sys{”、“#[”,等等)。我目前在3.0.5.RELEASE上 有没有想过为什么会这样呢?更复杂的是,我有一个第三个PropertyPlaceHolderConfiguration,所以只有两个前缀不起作用Java 为什么Spring3.x会忽略PropertyPlaceHolderConfigure的某些占位符前缀?,java,spring,Java,Spring,我有下面的bean定义。如果我将“exposeSystemProperties”bean的占位符前缀更改为“${”,并在第二个bean的属性路径中使用该前缀,则该前缀有效。如果我将其更改为“%{”以外的任何内容,则该前缀无效。我不能使用任何其他字符串(例如“$sys{”、“#[”,等等)。我目前在3.0.5.RELEASE上 有没有想过为什么会这样呢?更复杂的是,我有一个第三个PropertyPlaceHolderConfiguration,所以只有两个前缀不起作用 <bean id=
<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>
$sys{
类路径:properties/$sys{deploy.env}/client.properties
因为您需要前缀来控制特定于环境的属性,所以可以通过使用系统变量(而不是示例中的deploy.env
属性)来实现这一点:
如果您对“展望未来”持开放态度,另一种方法是使用Spring3.1,其中bean可以是特定于概要文件的。例如:
<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>
你是说Spring将系统变量加载到上下文中?我想你需要用另一种方式公开它。@Spencer,是的:
系统
/环境
变量在创建应用程序上下文时可供Spring使用,因此你可以依靠它们来驱动环境。这些变量可以用多种方式设置,例如le:System.setProperty(“环境系统”、“质量保证”)
或者您可以简单地通过-D
JVM参数传递它:-DENV_SYSTEM=qa
这当然可以解决我的问题,但是仍然不一定能解决为什么配置程序不能正确解析的问题。我还没有时间真正调试这个内联,但是当我调试时,这是我列表上的第一件事。谢谢r不同的视点。当然。一旦您尝试了它,它就会起作用,您就不会返回:)因为环境变量和/或配置文件更干净,更直观地在不同环境之间切换
<value>classpath:properties/dev/client.properties</value>
<value>classpath:properties/qa/client.properties</value>
<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>
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.getEnvironment().setActiveProfiles( "dev" );
ctx.load( "classpath:/org/boom/bang/config/xml/*-config.xml" );
ctx.refresh();