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="10" />
<!-- 线程池维护线程所同意的空暇时间 -->
<property name="keepAliveSeconds" value="0" />
<!-- 线程池维护线程的最大数量 -->
<property name="maxPoolSize" value="10" />
<!-- 线程池所使用的缓冲队列 -->
<property name="queueCapacity" value="0" />
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${squirrel_jdbc_driver}" />
<property name="url" value="${squirrel_jdbc_url}" />
<property name="username" value="${squirrel_jdbc_username}" />
<property name="password" value="${squirrel_jdbc_password}" />
<property name="validationQuery" value="select 1" />
<property name="initialSize" value="5" />
<property name="testWhileIdle" value="true" />
<property name="maxIdle" value="20" />
<property name="minIdle" value="5" />
<property name="maxActive" value="50" />
<property name="removeAbandonedTimeout" value="180" />
<property name="maxWait" value="30000" />
</bean> <bean id="ysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" >
<list>
<value>classpath*:mybatis/*.xml</value>
</list>
</property>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="ysqlSessionFactory"></constructor-arg>
<constructor-arg index="1" value="BATCH"></constructor-arg>
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.yowu.dao, com.yowu.app.repository.dao"/>
<!--核心就是加入以下一句。 后面那个属性是value。不是ref,切记-->
<property name="sqlSessionFactoryBeanName" value="ysqlSessionFactory" />
</bean>

通过查看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无法完成更换任务的更多相关文章

  1. 使用宏定义来减少JNI的繁琐

    本篇文章由:http://www.sollyu.com/use-macro-definitions-to-reduce-tedious-jni/ 说明 相信写过cocos2d-x的朋友,或者写过jni ...

  2. jQuery美化下拉菜单插件dropkick

    dropkick是一款基于jquery库的美化下拉框下拉菜单的插件,它通过定制HTML插入可使丑陋无聊的<select>下拉列表变得美丽. name属性是唯一一个必需的填写的,不过你也应该 ...

  3. CSS3_概述、发展史、模块介绍、与浏览器之间的关系

    一.CSS3概述和CSS3的发展史: 1.css3概述: CSS3是CSS2的升级版本,3只是版本号,它在CSS2.1的基础上增加了很多强大的新功能.    目前主流浏览器chrome.safari. ...

  4. Java基础(6)- 面向对象解析

    java面向对象 对象 知识点 java 的方法参数是按值调用,是参数的一份拷贝 封装 使用private将 属性值/方法 隐藏,外部只能调用 get,set方法/非private 的接口 获取 重载 ...

  5. Python-SMTP发送邮件(HTML、图片、附件)

    前言: SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式. 一.Python发送HTML ...

  6. .net推送微信消息模板

    1.获取access_token public string GetAccess_Token() { string appid = WxPayConfig.APPID; string appsecre ...

  7. Activity-ListView

    在手机中经常有列表方式.如果Activity中只有唯一一个List(这也是通常的情况),可以继承ListActivity来实现.我们用两个例子来学习List. List例子一:利用Android自带的 ...

  8. javaEE(10)_jdbc基本使用

    一.JDBC简介 1.SUN公司为了简化.统一对数据库的操作,定义了一套Java操作数据库的规范,称之为JDBC,JDBC(Java Data Base Connectivity,java数据库连接) ...

  9. USB工业摄像头设计之上位机

    在工业相机中对摄像头要求较高,且采集的图像数据要求是源数据,未经过任何处理. 为了兼容xp.win7(32bit 64bit) 程序采用VS2008  MFC编制,参考网上一些应用. CYUSB驱动与 ...

随机推荐

  1. 一起来开发Android的天气软件(四)——使用Gson解析数据

    离上一篇文章过去才4.5天,我们赶紧趁热打铁继续完毕该系列的天气软件的开发. 承接上一章的内容使用Volley实现网络的通信.返回给我们的是这一串Json数据{"weatherinfo&qu ...

  2. xamarin跨平台iOS、Android 与Windows

    http://xamarin.csdn.net/ Xamarin是一个行动App开发平台,提供跨平台开发能力,开发人员透过Xamarin开发工具与程序语言,即可开发出iOS.Android 与Wind ...

  3. POJ 2411

    Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9614   Accepted: 5548 ...

  4. android4.0 USB Camera示例(五个辅助)jpg压缩

    前的最后一个 我们说,一个直接yuv变成jpg该功能 但是转换不成功 主要功能是yuv420转jpg的 根据研究发现 yuv420的序列是这种 YYYY YYYY UVUV 而yuv422的隔行扫描的 ...

  5. lightoj1038(期望dp)

    给定一个数字d,随机选择一个d的约数,然后让d除以这个约数,形成新的d,不断继续这个步骤,知道d=1为止, 要我们求将d变为1的期望次数 设d1,d2...dj是除以约数后,形成的行的d,且dj==d ...

  6. hibernate学习——Set集合配置

    Set集合的配置 数据表的创建:表关系一个员工拥有多个身份 create table EMPLOYEE ( id INT NOT NULL auto_increment, first_name VAR ...

  7. 【ALearning】第四章 Android Layout组件布局(一)

    在本章中,我们将Android学习组件布局.在前面的章节,我们也开始使用LinearLayout布局.然后我们在布局文件更加具体的学习和理解,会. Android的界面是有布局和组件协同完毕的,布局好 ...

  8. 基础知识(1)- Java程序设计概述

    1.1 Java程序设计平台 1.2 Java“白皮书”的关键术语  1.2.1 简单性  1.2.2 面向对象  1.2.3 网络技能  1.2.4 健壮性  1.2.5 安全性  1.2.6 体系 ...

  9. 教你如何使用U盘装系统

    首先,你必须有一个4G以上U菜,然后,U光盘制作软件(这里我们使用url=KRVS0FUdaNAMKPUXUxjEijxBMalUjaJHph-tL-x4gXGSwVNUW3fj6RfuZtrMg1Y ...

  10. 前端构建工具gulp

    前端构建工具gulp使用   前端自动化流程工具,用来合并文件,压缩等. Gulp官网 http://gulpjs.com/ Gulp中文网 http://www.gulpjs.com.cn/ Gul ...