今天关注项目中的各配置文件参数设置是否恰当,然后就发现数据源是直接把各参数配置在applicationContent.xml文件当中,同时项目中其它模块又用到了properties配置文件引入属性值的做法,于是就想把数据源配置的参数也迁移到properties配置文件中来,便于以后的修改。
由于使用的是springmvc框架(spring3.1.1+mybatis3.1.1+mybatis-spring-1.1.1),所以就在applicationContent.xml中配置PropertyPlaceholderConfigurer来加载properties配置文件,谁想这种以前在项目中应用很好使的方式今天怎么也通不过,配置完成,重新部署后就报如下错误:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in ServletContext resource [/****/application-context.xml]: Initialization of bean failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource[/***/application-context.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/*****/application-context.xml]: Initialization of bean failed;
nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'checkoutTimeout';
nested exception is java.lang.NumberFormatException: For input string: "${cpool.checkoutTimeout}"
。。。。。。
一开始怀疑是spring版本问题,就搜索了一下“spring3 PropertyPlaceholderConfigurer”,结果发现还真有类似的提问,只不过比我问的更准确,一下就把问题定位到了问题的根源-------mybatis下的MapperScannerConfigurer扫描模式造成了bean的加载顺序改变从而使得PropertyPlaceholderConfigurer无法正常加载。
具体说来就是,myabatis使用MapperScannerConfigurer扫描模式后他会优先于PropertyPlaceholderConfigurer执行,所以这个时候,${cpool.checkoutTimeout}还没有被properties文件里面的值所替换,所以出现TypeMismatchException,然后就异常了
知道了异常原因所在,那么问题解决结会快一些了,于是按照相关搜索,查阅下面的帖子,基本找到解决方案:
http://www.oschina.net/question/188964_32305
当然,毕竟不是一模一样的错误,还是需要做一些调整,下面把调整的内容列出来,希望对同样问题的朋友有帮助,请留意标红的地方:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:xxxxx.properties</value> </list> </property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="autoCommitOnClose" value="true"/> <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/> </bean> <!--只要下面bean的id不叫sqlSessionFactory,就成--> <bean id="ysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="configLocation" value="classpath:sqlMapConfig.xml"/> <property name="mapperLocations" > <list> <value>classpath:com/xxx/xxx/xxx/*.xml</value> <value>classpath:com/xxx/xxx/xxx/*.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.xxx.xxx.dao,com.xxx.xxx.xxx.dao,com.xxx.xxx.xxx.dao"/> <!--核心就是添加下面一句。后面那个属性是value,不是ref,切记--> <property name="sqlSessionFactoryBeanName" value="ysqlSessionFactory" /> </bean>
在3.1.1版本中配置的时候提供了以下属性值关联:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xxxx.dal.mapper" />
<property name="sqlSessionFactoryBeanName" value="ysSqlSessionFactory" />
<!--<property name="sqlSessionFactory" ref="ysSqlSessionFactory"></property> -->
</bean>
改用sqlSessionFactoryBeanName注入就没有问题(不要使用sqlSessionFactory属性注入,使用sqlSessionFactoryBeanName注入),因为这时不会立即初始化sqlSessionFactory,传入的只是名字,非bean,所以不会引发提前初始化问题。。
最后再备注两个链接:
PropertyPlaceholderConfigurer介绍和应用
http://www.cnblogs.com/yl2755/archive/2012/05/06/2486752.html
Mybatis学习系列:
相关推荐
Spring属性占位符PropertyPlaceholderConfigurer的使用,自己写的一个demo
Spring中PropertyPlaceholderConfigurer的使用
NULL 博文链接:https://wokeke.iteye.com/blog/2214113
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:/deploy.properties <!-- 配置sessionFactory <value>classpath:hibernate...
主要介绍了Spring如何使用PropertyPlaceholderConfigurer读取文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
NULL 博文链接:https://chyy001.iteye.com/blog/1338488
部分配置代码,所有代码都有,很全 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">...-- 配置数据源 --> <value>classpath:SqlMapConfig.xml </bean>
什么 与通过从属性文件读取其配置来进行自我配置相比,对于应用程序而言,有什么可能更简单? 好吧,对于业务应用程序而言... 首先,我们将使用PropertyPlaceholderConfigurer通过读取存储在.properties文件中的属性来
9.9. 公共问题的解决方案 9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 ...
defaultTargetDataSource这个对象,根据自己需求的情况默认的情况给一个默认的数据源. --> <!-- read --> ,get,select,count,list,query" /> <!-- write --> ,add,insert,create,...
JDBCTemp+MySQL示例。Spring,PropertyPlaceholderConfigurer,BeanPropertyRowMapper
-- 数据源配置,在生产环境使用应用服务器的数据库连接池 --> <!-- <jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/store" />--> <!--Hibernate配置--> ...
它提供了很多方面的功能,比如依赖注入、面向方面编程(AOP)、数据访问抽象及ASP.NET扩展等等。Spring.NET以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。 第一章 序言 第二章 ...
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="/WEB-INF/red5-web.properties" /> <!-- ROOT web ...
4.8.2.1 示例:PropertyPlaceholderConfigurer. 82 4.8.2.2 示例:PropertyOverrideConfigurer 83 4.8.3 使用FactoryBean 来自定义实例化逻辑. 84 4.9 基于注解的容器配置. 85 4.9.1 @Required....
View层技术的解决方案,您甚至可以定义自己的View层技术实现。 JSP View 层 当使用 JSP 作为 View 层技术时,您可以结合 JSTL 以及 Spring 提供的标签。 结合 JSTL <spring:bind> 标签 数据绑定...
我们可以从 /WEB-INF/jdbc.properties 中找到这个变量的值. 如果你的数据库已经配置好, 就将第一个注释去掉. jdbc.properties: 1 jdbc.driverClassName=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://...
#Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...