PropertySourcesPlaceholderConfigurer 优化建议
-
当用户没有配置
PropertySourcesPlaceholderConfigurer时,Apollo会帮我们构建一个该Bean -
Apollo是否可以添加PropertySourcesPlaceholderConfigurer 的order,让其优先级高于Ordered.LOWEST_PRECEDENCE -
因为有很多用户 会如下配置
-
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="OVERRIDE" />
-
-
该配置导致Spring 创建
PropertyPlaceholderConfigurer并且 执行早于Apollo配置的PropertySourcesPlaceholderConfigurer,会导致报错 -
如果设置
order,这样就可以保证Spring优先使用Apollo配置的PropertySourcesPlaceholderConfigurer,处理Bean中占位符。 -
可以让很多用户少走弯路
感谢建议!
不知是否能提供一个简单的demo演示一下具体的问题?
如果能同时提交一个pull request是最好的~
感谢建议!
不知是否能提供一个简单的demo演示一下具体的问题?
如果能同时提交一个pull request是最好的~
- 比如有如下配置
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="OVERRIDE"/>
-
有可能用户会指定
system-properties-model属性,该属性会导致Spring自动装配``PropertyPlaceholderConfigurer` ,从而导致无法解决占位符的问题。 -
如果上述配置修改成
-
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="OVERRIDE"/> <!-- external property files --> <context:property-placeholder location="classpath*:*.properties" order="-105112121" /> -
该配置只是新增
order属性,使得优先级高于最上面的<context:配置的 -
这样Spring就会优先执行
PropertySourcesPlaceholderConfigurer,继而可以使用Apollo中的变量解决占位符
-
-
所以
-
建议Spring中没有配置
PropertySourcesPlaceholderConfigurer类型的Bean时,Apollo配置的PropertySourcesPlaceholderConfigurer指定order-
代码如下
-
在
com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil中添加如下代码-
BeanDefinition annotationProcessor = BeanDefinitionBuilder.genericBeanDefinition(beanClass).getBeanDefinition(); if (PropertySourcesPlaceholderConfigurer.class.isAssignableFrom(beanClass)){ annotationProcessor.getPropertyValues().addPropertyValue("order", Ordered.HIGHEST_PRECEDENCE+5); } registry.registerBeanDefinition(beanName, annotationProcessor);
-
-
-
-
感谢建议!
order看上去对这个bean比较重要,如果hard code的话,以后有可能还会碰到此类问题,对此是否有啥好的建议?
这个硬编码形式是否有点过于粗暴,可以让用户自行在注解里设置order。硬编码方式会导致用户自定义的PropertySourcesPlaceholderConfigurer晚于apollo定义的,进而处理value报错