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

来源:互联网 发布:赵丽颖演技知乎 编辑:程序博客网 时间:2024/06/10 15:33

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

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class CustomPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {  
  2.     private Resource[] locations;  
  3.   
  4.     public void loadProperties(Properties props) throws IOException {  
  5.         if (this.locations != null) {  
  6.             PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();  
  7.             for (int i = 0; i < this.locations.length; i++) {  
  8.                 Resource location = this.locations[i];  
  9.                 if (logger.isInfoEnabled()) {  
  10.                     logger.info("Loading properties file from " + location);  
  11.                 }  
  12.                 InputStream is = null;  
  13.                 try {  
  14.                     is = location.getInputStream();  
  15.                     propertiesPersister.load(props, is);  
  16.   
  17.                     Configuration conf = SquirrelConfiguration.create();  
  18.                     Map<String, String> map = conf.listAllConfEntry();  // 从squirrel-site.xml中读取配置信息  
  19.                     for (Map.Entry<String, String> entry : map.entrySet()) {  
  20.                         props.put(entry.getKey(), entry.getValue());  
  21.                     }  
  22.                 } finally {  
  23.                     if (is != null) is.close();  
  24.                 }  
  25.             }  
  26.         }  
  27.     }  
  28.   
  29.     public void setLocations(Resource[] locations) {  
  30.         super.setLocations(locations);  
  31.         this.locations = locations;  
  32.     }  
  33.   
  34. }  




但是运行却一直报错,${jdbc_driver_name}没有被替换,通过查询发现,原来是在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致PropertyPlaceholderConfigurer失效,也就是用${jdbc_driver_name}这样之类的表达式,将无法获取到properties文件里的内容。 导致这一原因是因为,MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,
PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。 但如果不设置sqlSessionFactory 属性的话,就必须要保证sessionFactory在spring中名称一定要是sqlSessionFactory ,否则就无法自动注入。又或者直接定义MapperFactoryBean ,再或者放弃自动代理接口方式。
可以如下方式修改:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <bean id="propertyConfigurer"  class="com.yowu.common.CustomPropertyPlaceholderConfigurer">  
  2.         <property name="locations">  
  3.             <list>  
  4.                 <value>classpath:important.properties</value>  
  5.             </list>  
  6.         </property>  
  7.      </bean>  
  8.   
  9.     <!-- 配置线程池 -->  
  10.     <bean id="taskExecutor"  
  11.         class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
  12.         <!-- 线程池维护线程的最少数量 -->  
  13.         <property name="corePoolSize" value="10" />  
  14.         <!-- 线程池维护线程所允许的空闲时间 -->  
  15.         <property name="keepAliveSeconds" value="0" />  
  16.         <!-- 线程池维护线程的最大数量 -->  
  17.         <property name="maxPoolSize" value="10" />  
  18.         <!-- 线程池所使用的缓冲队列 -->  
  19.         <property name="queueCapacity" value="0" />  
  20.     </bean>  
  21.   
  22.     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  23.         <property name="driverClassName" value="${squirrel_jdbc_driver}" />  
  24.         <property name="url" value="${squirrel_jdbc_url}" />  
  25.         <property name="username" value="${squirrel_jdbc_username}" />  
  26.         <property name="password" value="${squirrel_jdbc_password}" />  
  27.         <property name="validationQuery" value="select 1" />  
  28.         <property name="initialSize" value="5" />  
  29.         <property name="testWhileIdle" value="true" />  
  30.         <property name="maxIdle" value="20" />  
  31.         <property name="minIdle" value="5" />  
  32.         <property name="maxActive" value="50" />  
  33.         <property name="removeAbandonedTimeout" value="180" />  
  34.         <property name="maxWait" value="30000" />  
  35.     </bean>  
  36.   
  37.     <bean id="ysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  38.         <property name="dataSource" ref="dataSource"/>  
  39.         <property name="mapperLocations" >  
  40.             <list>  
  41.                 <value>classpath*:mybatis/*.xml</value>  
  42.             </list>  
  43.         </property>  
  44.     </bean>  
  45.     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">  
  46.         <constructor-arg index="0" ref="ysqlSessionFactory"></constructor-arg>  
  47.         <constructor-arg index="1" value="BATCH"></constructor-arg>  
  48.     </bean>  
  49.   
  50.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
  51.         <property name="basePackage" value="com.yowu.dao, com.yowu.app.repository.dao"/>  
  52.         <!--核心就是添加下面一句。后面那个属性是value,不是ref,切记-->  
  53.         <property name="sqlSessionFactoryBeanName" value="ysqlSessionFactory" />  
  54.     </bean>  


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

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {  
  2.     if (this.processPropertyPlaceHolders) {  
  3.       processPropertyPlaceHolders();  
  4.     }  
  5.   
  6.     Scanner scanner = new Scanner(beanDefinitionRegistry);  
  7.     scanner.setResourceLoader(this.applicationContext);  
  8.   
  9.     scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));  
  10.   }  
  11.   
  12.   /* 
  13.    * BeanDefinitionRegistries are called early in application startup, before 
  14.    * BeanFactoryPostProcessors. This means that PropertyResourceConfigurers will not have been 
  15.    * loaded and any property substitution of this class' properties will fail. To avoid this, find 
  16.    * any PropertyResourceConfigurers defined in the context and run them on this class' bean 
  17.    * definition. Then update the values. 
  18.    */  
  19.   private void processPropertyPlaceHolders() {  
大概意思是可以设置processPropertyPlaceHolders为true,强制让PropertyResourceConfigure执行下替换工作,大家不妨试一下,不失为一个优雅的解决方案。
0 0
原创粉丝点击