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. MyReport演示下载连接和相关文章索引

    演示地址 Flex集成方式 (旧版2.6) HTML集成方式 MyReport产品站点 NEW 相关文章 ------------------------2.6下面版本号--------------- ...

  2. 强势围观,CSDN代码引用bug

    看我写的一篇blog  http://blog.csdn.net/laijieyao/article/details/41014355,在代码上引用了微软雅黑的字体,结果代码显示出来把我给惊呆了 竟然 ...

  3. Cordova CLI源码分析(三)——初始化

    本部分主要涉及以下三个文件 1 cli.js 2 cordova.js 3 events.js 通过前一篇package.json的分析,可以知道,当命令行执行cordova相关命令时,首先调用mai ...

  4. 在自己的base脚本中实现自动补全

    在90年代Linux和DOS共存的年代里,Linux的Shell们有一个最微不足道但也最实用的小功能,就是命令自动补全.而DOS那个笨蛋一直到死都没学会什么叫易用. Linux的这个微不足道的小传统一 ...

  5. UVA 1513 - Movie collection(树状数组)

    UVA 1513 - Movie collection option=com_onlinejudge&Itemid=8&page=show_problem&category=5 ...

  6. 【Android进阶】为什么要创建Activity基类以及Activity基类中一般有哪些方法

    现在也算是刚刚基本完成了自己的第一个商业项目,在开发的过程中,参考了不少人的代码风格,然而随着工作经验的积累,终于开始慢慢的了解到抽象思想在面向对象编程中的重要性,这一篇简单的介绍一下我的一点收获. ...

  7. git tag使用

    #git tag command git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]       ...

  8. 【原创】纯OO:从设计到编码写一个FlappyBird (四)

    第三部分请点这里 这里来实现Obstacle类.其实flappybird的本质就是小鸟原地掉,然后几根柱子在走.这也是在Game类里,用obs.move()来实现游戏逻辑的原因. 我们首先必须确定几个 ...

  9. 20140719中国互联网公司市值排名TOP20

    近期在找工作.关注了一下中国互联网公司的市值,实际情况跟想象的区别非常大. 比方异军突起的小米.京东.唯品会.聚美优品. 比方乐视网由于政策原因,市值两日缩水10亿$.停牌了. 搜房网市值90天蒸发3 ...

  10. poj 2935 Basic Wall Maze

    是一个图论的基础搜索题- 没什么好说的就是搜索就好 主要是别把 代码写的太屎,错了不好找 #include<cstdio> #include<algorithm> #inclu ...