这是一个 Spring 项目,
第一个 PropertyPlaceholderConfigurer ,在 context-aaa.xml 主要是配置 JDBC properties
<bean id="jdbcPropertyPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="0"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="location">
<value>classpath:jdbc.properties</value>
</property>
</bean>
其中,jdbc.properties 的内容如下:
test.datasource.url=jdbc:mysql://192.168.0.66:3306/dev
test.datasource.username=root
test.datasource.password=123456
第二个 PropertyPlaceholderConfigurer ,在 context-bbb.xml ,功能是从数据里加载 properties
<bean id="commonsConfigurationPropertyPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="properties" ref="commonsConfigurationFactoryBean"/>
</bean>
<bean name="commonsConfigurationFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="configuration"/>
</bean>
<bean id="configuration" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource" ref="druidDataSource"/>
<constructor-arg index="1" value="configuration"/>
<constructor-arg index="2" value="name"/>
<constructor-arg index="3" value="value"/>
</bean>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${test.datasource.url}"/>
<property name="username" value="${test.datasource.username}"/>
<property name="password" value="${test.datasource.password}"/>
</bean>
问题来了,加载 DruidDataSource 时,占位符并没有被替换。
我已经加了 PropertyPlaceholderConfigurer 的 order 属性排序,以及 ignoreUnresolvablePlaceholders 为 true 了,但是问题依旧。
DruidDataSource 作为第二个 PropertyPlaceholderConfigurer 的依赖,它的配置占位符不会被处理吗?
我断点发现,BeanFactoryPostProcessor 的处理,在程序的报错之后才执行,是怎么回事?
https://jira.spring.io/browse...
我找到了解决方案:
The alternative is to
(a) use the new PropertySourcesPlaceholderConfigurer instead of the traditional PropertyPlaceholderConfigurer;
(b) eliminate the first PropertySourcesPlaceholderConfigurer ;
(c) register a PropertySource with the ApplicationContext's Environment that contains the properties for the placeholders that need replacement in the PropertySourcesPlaceholderConfigurer
我想问第三步怎么操作啊?
检查一下是否是加了 PropertyPlaceholderConfigurer 的 order 属性排序,以及 ignoreUnresolvablePlaceholders 为 true 后xml文件没有生效的原因。