Java 一个Spring PropertyPlaceHolderConfigure可以配置另一个吗?
我有一个PropertyPlaceHolderConfiguration,如下所示:Java 一个Spring PropertyPlaceHolderConfigure可以配置另一个吗?,java,spring,servlets,Java,Spring,Servlets,我有一个PropertyPlaceHolderConfiguration,如下所示: <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="ignoreUnresolvablePlaceholders" value="true"/> <
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:assuredlabor/margarita-${runningMode}.properties</value>
</list>
</property>
</bean>
<context-param>
<param-name>runningMode</param-name>
<param-value>production</param-value>
</context-param>
类路径:assuredlabor/margarita-${runningMode}.properties
我希望能够在web.xml中指定我的运行模式,如下所示:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:assuredlabor/margarita-${runningMode}.properties</value>
</list>
</property>
</bean>
<context-param>
<param-name>runningMode</param-name>
<param-value>production</param-value>
</context-param>
运行模式
生产
所以我把这个bean放在我上面描述的“main”属性bean之上:
<bean id="servletPropertyPlaceholderConfigurer" class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
</bean>
但这似乎不起作用
这在春天是可能的吗?我现在正在使用2.5版
我发现了类似的问题:
但是没有讨论ServletContextPropertyPlaceHolderConfigure,因此我认为这是一个合理的问题。来自:
PropertyPlaceHolderConfigure的子类,将占位符解析为ServletContext init参数(即web.xml上下文参数条目)
除了web.xml上下文参数外,还可以与“位置”和/或“属性”值组合。或者,可以在没有本地属性的情况下定义,以将所有占位符解析为web.xml上下文参数(或JVM系统属性)
如果无法根据应用程序中提供的本地属性解析占位符,则此配置程序将退回到ServletContext参数。还可以配置为让ServletContext init参数覆盖本地属性(contextOverride=true)
(可选)支持搜索ServletContext属性:如果启用,则将使用其字符串化值(如果找到)将无法解析的占位符与相应的ServletContext属性进行匹配。这可以用来将动态值输入到Spring的占位符解析中
如果未在WebApplicationContext(或能够满足ServletContextAware回调的任何其他上下文)中运行,则此类的行为将类似于默认的PropertyPlaceHolderConfigure。这允许在测试套件中保留ServletContextPropertyPlaceHolderConfigure定义
据我所知,这意味着您可以只使用一个配置器:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:assuredlabor/margarita-${runningMode}.properties</value>
</list>
</property>
</bean>
类路径:assuredlabor/margarita-${runningMode}.properties
在Spring2中,如果没有一些自定义编码,您就无法做到这一点,因为一个属性占位符无法配置另一个属性占位符
你需要用弹簧3把它从盒子里拿出来。要实现这一点,您必须创建一个bean,它以某种方式具有您想要的值,并在设置属性占位符时使用spring el引用该spring。有一个特殊的bean用于获取各个servlet上下文参数,如下所示:
<bean id="runningMode" class="org.springframework.web.context.support.ServletContextAttributeFactoryBean">
<property name="attributeName" value="runningMode" />
</bean>
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<list>
<value>classpath:assuredlabor/margarita-#{runningMode}.properties</value>
</list>
</property>
</bean>
类路径:assuredlabor/margarita-#{runningMode}.properties
然后,您可以引用正常${}语法中的任何属性