- 33
- 0
这是一个 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
我想问第三步怎么操作啊?
- 共 0 条
- 全部回答
-
╭ァ回忆ゝ那些往事ゞ 普通会员 1楼
在Spring框架中,
PropertyPlaceholderConfigurer是一个Bean后处理器,用于从属性文件中解析和替换Bean定义中的占位符(placeholder)。然而,如果在同一个Spring容器中配置了多个PropertyPlaceholderConfigurer,可能会出现占位符未替换的问题。这是因为当存在多个
PropertyPlaceholderConfigurer时,Spring容器会按顺序处理它们。第一个处理器会替换它能找到的所有占位符,而后续的处理器可能无法找到需要替换的占位符,因为它们已经被前面的处理器处理过了,或者它们查找的属性文件中没有对应的键值对。解决办法:
- 合并属性源:尽量在一个
PropertyPlaceholderConfigurer中加载所有需要的属性文件,避免使用多个处理器。
xml <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config.properties</value> <value>classpath:db.properties</value> </list> </property> </bean>-
优先级设置:若确实需要使用多个处理器,并且有优先级关系,可以考虑通过设置
order属性来控制它们的执行顺序。 -
使用
Environment和@Value注解:在Spring 3.1及以上版本中,推荐使用@Configuration类结合@PropertySource以及@Value注解进行属性注入,这种方式更为灵活且不会遇到上述问题。
```java @Configuration @PropertySource("classpath:config.properties") public class AppConfig {
@Autowired private Environment env; @Value("${property.name}") private String propertyName;} ```
- 合并属性源:尽量在一个
- 扫一扫访问手机版
回答动态

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器更新之后。服务器里面有部分玩家要重新创建角色是怎么回事啊?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题函数计算不同地域的是不能用内网吧?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题ARMS可以创建多个应用嘛?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题在ARMS如何申请加入公测呀?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题前端小程序接入这个arms具体是如何接入监控的,这个init方法在哪里进行添加?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器刚到期,是不是就不能再导出存档了呢?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器的游戏版本不兼容 尝试更新怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器服务器升级以后 就链接不上了,怎么办?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器转移以后服务器进不去了,怎么解决?预计能赚取 0积分收益

- 神奇的四哥:发布了悬赏问题阿里云幻兽帕鲁服务器修改参数后游戏进入不了,是什么情况?预计能赚取 0积分收益
- 回到顶部
- 回到顶部
