首页 > 代码库 > 自定义PropertyPlaceHolder无法完成替换任务

自定义PropertyPlaceHolder无法完成替换任务

 Spring默认的PropertyPlaceholderConfigurer只能加载properties格式的配置文件,现在需要完成让其支持可以从类似hadoop格式的xml配置文件中读取配置信息,并替换掉相关bean中的占位符,对其进行了扩展,具体扩展如下:

public class CustomPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    private Resource[] locations;

    public void loadProperties(Properties props) throws IOException {
        if (this.locations != null) {
            PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
            for (int i = 0; i < this.locations.length; i++) {
                Resource location = this.locations[i];
                if (logger.isInfoEnabled()) {
                    logger.info("Loading properties file from " + location);
                }
                InputStream is = null;
                try {
                    is = location.getInputStream();
                    propertiesPersister.load(props, is);

                    Configuration conf = SquirrelConfiguration.create();
                    Map<String, String> map = conf.listAllConfEntry();  // 从squirrel-site.xml中读取配置信息
                    for (Map.Entry<String, String> entry : map.entrySet()) {
                        props.put(entry.getKey(), entry.getValue());
                    }
                } finally {
                    if (is != null) is.close();
                }
            }
        }
    }

    public void setLocations(Resource[] locations) {
        super.setLocations(locations);
        this.locations = locations;
    }

}




但是运行却一直报错,${jdbc_driver_name}没有被替换,通过查询发现,原来是在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致PropertyPlaceholderConfigurer失效,也就是用${jdbc_driver_name}这样之类的表达式,将无法获取到properties文件里的内容。 导致这一原因是因为,MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,
PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。 但如果不设置sqlSessionFactory 属性的话,就必须要保证sessionFactory在spring中名称一定要是sqlSessionFactory ,否则就无法自动注入。又或者直接定义MapperFactoryBean ,再或者放弃自动代理接口方式。
可以如下方式修改:
<bean id="propertyConfigurer"  class="com.yowu.common.CustomPropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:important.properties</value>
            </list>
        </property>
     </bean>

	<!-- 配置线程池 -->
	<bean id="taskExecutor"
		class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
		<!-- 线程池维护线程的最少数量 -->
		<property name="corePoolSize" value=http://www.mamicode.com/"10" />>

通过查看MapperScannerConfigurer源码发现,其实有这么一段代码:

public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
    if (this.processPropertyPlaceHolders) {
      processPropertyPlaceHolders();
    }

    Scanner scanner = new Scanner(beanDefinitionRegistry);
    scanner.setResourceLoader(this.applicationContext);

    scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
  }

  /*
   * BeanDefinitionRegistries are called early in application startup, before
   * BeanFactoryPostProcessors. This means that PropertyResourceConfigurers will not have been
   * loaded and any property substitution of this class' properties will fail. To avoid this, find
   * any PropertyResourceConfigurers defined in the context and run them on this class' bean
   * definition. Then update the values.
   */
  private void processPropertyPlaceHolders() {
大概意思是可以设置processPropertyPlaceHolders为true,强制让PropertyResourceConfigure执行下替换工作,大家不妨试一下,不失为一个优雅的解决方案。

自定义PropertyPlaceHolder无法完成替换任务