欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mybatis xml配置文件读取不了 properties的属性 博客分类: Java mybatis xml 注入不了 ${} properties 数据源 

程序员文章站 2024-02-12 15:43:10
...
问题描述:

今天关注项目中的各配置文件参数设置是否恰当,然后就发现数据源是直接把各参数配置在spring-dao.xml文件当中,同时项目中其它模块又用到了properties配置文件引入属性值的做法,于是就想把数据源配置的参数也迁移到properties配置文件中来,便于以后的修改。

  由于使用的是springmvc框架(spring3.1.1+mybatis3.1.1+mybatis-spring-1.1.1),所以就在applicationContent.xml中配置PropertyPlaceholderConfigurer来加载properties配置文件,谁想这种以前在项目中应用很好使的方式今天怎么也通不过,配置完成,重新部署后就报如下错误:

2016-03-17 10:12:12,217  INFO (com.zsj.authcenter.controller.AuthController:106) - ---authcenter is fail---
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${jdbc.driverClassName}'
### The error may exist in com/zsj/authcenter/dao/conf/unite_user.xml
### The error may involve com.zsj.authcenter.dao.mysql.UniteUserDao.findUniteUser
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${jdbc.driverClassName}'
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
at com.sun.proxy.$Proxy9.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:160)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:95)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
at com.sun.proxy.$Proxy10.findUniteUser(Unknown Source)
at com.zsj.authcenter.service.impl.AuthServiceImpl.login(AuthServiceImpl.java:36)
at com.zsj.authcenter.controller.AuthController.doLogin(AuthController.java:155)

一开始怀疑是spring版本问题,就搜索了一下“spring3  PropertyPlaceholderConfigurer”,结果发现还真有类似的提问,只不过比我问的更准确,一下就把问题定位到了问题的根源-------mybatis下的MapperScannerConfigurer扫描模式造成了bean的加载顺序改变从而使得PropertyPlaceholderConfigurer无法正常加载。

具体说来就是,myabatis使用MapperScannerConfigurer扫描模式后他会优先于PropertyPlaceholderConfigurer执行,所以这个时候,${cpool.checkoutTimeout}还没有被properties文件里面的值所替换,所以出现TypeMismatchException,然后就异常了

知道了异常原因所在,那么问题解决结会快一些了,于是按照相关搜索,查阅下面的帖子,基本找到解决方案:

具体spring-dao.xml配置文件如下 :


<!--只要下面bean的id不叫sqlSessionFactory,就成-->
<bean id="ysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
  <property name="configLocation" value="classpath:com/zsj/authcenter/dao/conf/mybatis-config.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">   
    <property name="basePackage" value="com.zsj.authcenter.dao.mysql" />

<!--核心就是添加下面一句。后面那个属性是value,不是ref,切记-->
     <property name="sqlSessionFactoryBeanName" value="ysqlSessionFactory"/>
</bean>

改用sqlSessionFactoryBeanName注入就没有问题(不要使用sqlSessionFactory属性注入,使用sqlSessionFactoryBeanName注入),因为这时不会立即初始化sqlSessionFactory,传入的只是名字,非bean,所以不会引发提前初始化问题。。

修改好了之后,满心欢喜的以为解决了问题,于是就部署,启动,结果又报如下错误:

2016-03-17 10:12:12,217  INFO (com.zsj.authcenter.controller.AuthController:106) - ---authcenter is fail---
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.  Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${jdbc.driverClassName}'
### The error may exist in com/zsj/authcenter/dao/conf/unite_user.xml
### The error may involve com.zsj.authcenter.dao.mysql.UniteUserDao.findUniteUser
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${jdbc.driverClassName}'
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:365)
at com.sun.proxy.$Proxy9.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:160)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:95)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:40)
at com.sun.proxy.$Proxy10.findUniteUser(Unknown Source)
at com.zsj.authcenter.service.impl.AuthServiceImpl.login(AuthServiceImpl.java:36)
at com.zsj.authcenter.controller.AuthController.doLogin(AuthController.java:155)


问题是一样的,问题似乎没有解决。可大部分资料都是这样说的啊,难道是我漏掉了什么吗?
经过仔细排查,果真有漏掉的地方,如下:

在spring-dao配置文件里面的解析标签是:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"

default-autowire="byType">

注意红色部分。


解决方案,将解析标签的红色部分干掉就行了。

有的文件写的是default-autowire="byName",同理,干掉就可以了。

至此,在启动部署,成功解决问题.大功告成.