`
chenruieye
  • 浏览: 36670 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

mybatis xml配置文件读取不了 properties的属性

    博客分类:
  • Java
阅读更多
问题描述:

今天关注项目中的各配置文件参数设置是否恰当,然后就发现数据源是直接把各参数配置在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",同理,干掉就可以了。

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










分享到:
评论
1 楼 天地炫舞 2018-05-18  
厉害啊,居然解决了,等会我也来做下笔记,谢谢

相关推荐

    springmybatis

    2. Configuration.xml 里面 的&lt;mapper resource="com/yihaomen/mybatis/model/User.xml"/&gt;是包含要映射的类的xml配置文件。 3. 在User.xml 文件里面 主要是定义各种SQL 语句,以及这些语句的参数,以及要返回的类型...

    高效Java数据访问组件Uncode-DAL全功能源码

    - 属性配置文件 (.properties): 9个 - Git忽略文件 (.gitignore): 3个 - INI配置文件 (.ini): 2个 - 许可证文件 (LICENSE): 1个 - Markdown文档 (.md): 1个 - 工厂配置文件 (factories): 1个 - YAML配置文件 (.yml):...

    MyBatis_Course:MyBatis 适合初学者学习

    v0.4 - MyBatis数据库的配置单独放在一个properties文件中 v0.5 - 为实体类定义别名,简化sql映射xml文件中的引用 v0.6 - 解决字段名与实体类属性名不相同的冲突 v0.7 - MyBatis中使用association标签来解决一对一的...

    SpringMVC-Mybatis-Shiro-redis-master 权限集成缓存中实例

    -- redis 配置,也可以把配置挪到properties配置文件中,再读取 --&gt; &lt;!-- 这种 arguments 构造的方式,之前配置有缺点。 这里之前的配置有问题,因为参数类型不一致,有时候jar和环境的问题,...

    基于ssm+Vue的ssm无纸化考试模拟系统(源码+部署说明+系统介绍+数据库).zip

    配置Spring和MyBatis:根据实际情况修改mybatis-config.xml和spring-mvc.xml文件中的配置信息,以便系统能够正确读取数据库表结构和执行SQL语句。启动项目:在命令行中进入项目根目录,执行./mvnw spring-boot:run...

    Java高级程序设计实战教程第三章-Java反射机制.pptx

    3.1 应用场景 在一些开源框架里,如Spring,Struts,Hibernate,MyBatis等,应用程序会提供一个配置文件如xml文件或者properties,然后在Java类里面解析xml或properties里面的内容,得到一个字符串,然后用反射机制...

    springBootDemo

    读取Properties 项目源码 Spring Data JPA 的使用 JPA简单概念 快速上手 基本查询 复杂查询 关联查询 项目源码 模板引擎Thymeleaf 简单上手 常用标签汇总 项目源码 JPA 和 Thymeleaf 实践 多环境配置...

    单点登录源码

    Spring+SpringMVC+Mybatis框架集成公共模块,包括公共配置、MybatisGenerator扩展插件、通用BaseService、工具类等。 &gt; zheng-admin 基于bootstrap实现的响应式Material Design风格的通用后台管理系统,`zheng`...

    SmartWx微信公众号管理系统-其他

    upload.properties 如属性文件所描述,如图片想放到项目中,res.upload.url注释即可 7、缓存设置。为兼容jdk1.7,此版本中暂时废弃j2cache,默认仅使用ehcache缓存,如想使用,修改J2CacheUtil,用CacheJ2Utils替换...

    jeesuite-libs-其他

    由(jeesuite-admin)输出兼容遗留kafka系统、支持发送和接收无封装的消息mybatis模块代码生成、自动CRUD、可无缝对接mybaits增强框架Mapper基于properties配置多数据源支持,无需修改XML读写分离,事务内操作强制读...

Global site tag (gtag.js) - Google Analytics