- 浏览: 36670 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
天地炫舞:
厉害啊,居然解决了,等会我也来做下笔记,谢谢
mybatis xml配置文件读取不了 properties的属性 -
YaLove:
你好,“建一个freestyle的Job,然后scm等配置也照 ...
Jenkins加Shell实现最简单的持续部署
问题描述:
今天关注项目中的各配置文件参数设置是否恰当,然后就发现数据源是直接把各参数配置在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",同理,干掉就可以了。
至此,在启动部署,成功解决问题.大功告成.
今天关注项目中的各配置文件参数设置是否恰当,然后就发现数据源是直接把各参数配置在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",同理,干掉就可以了。
至此,在启动部署,成功解决问题.大功告成.
发表评论
-
linux 安装es 7.6.2 集群
2021-01-20 10:42 01、我的3台linux机器是 我的创建的三台分 ... -
记一次mysql主从同步延迟解决方案
2020-08-27 16:01 01、背景:近期项目在一个查询模块,返回的数据时而有,时 ... -
记我的第一次spring aop项目实践
2020-04-17 16:46 376一、需求背景。 1 ... -
myeclipse 的properties文件显示不了中文的问题
2016-03-23 14:25 334操作步骤: Window-》Ppreference-》Gen ... -
spring AspectJ的Execution表达式
2015-12-03 09:43 625原址参考: http://blog.csdn. ... -
spring mvc 原理及应用
2015-11-09 13:37 618原址:http://blog.csdn.net/ ... -
Jenkins加Shell实现最简单的持续部署
2015-11-07 10:30 7863参考跳转链接: http://ju.outofmemor ... -
maven私服仓库搭建
2015-10-15 10:54 582参考:http://www.blogjava.net/ ... -
jms 应用示例教程
2015-10-13 15:50 347http://blog.csdn.net/quincylk/a ... -
Java JMS学习纪要
2015-10-13 15:36 474http://www.51testing.com/html/ ... -
java 打包可执行文件
2013-02-03 00:10 719java类文件存放路径 F:\BatchLoad.java ... -
JAVA面向对象概念之抽象与接口的意思以及区别
2012-02-22 23:04 789抽象类不可以被实例化 ... -
list 和数组相互转换
2011-10-09 09:09 804//数组转List List data = new Ar ... -
java监控服务器端应用程序
2011-09-16 17:55 1382最近测程序性能,需要远程监控服务端程序的运行情况,从网上找了很 ...
相关推荐
2. Configuration.xml 里面 的<mapper resource="com/yihaomen/mybatis/model/User.xml"/>是包含要映射的类的xml配置文件。 3. 在User.xml 文件里面 主要是定义各种SQL 语句,以及这些语句的参数,以及要返回的类型...
- 属性配置文件 (.properties): 9个 - Git忽略文件 (.gitignore): 3个 - INI配置文件 (.ini): 2个 - 许可证文件 (LICENSE): 1个 - Markdown文档 (.md): 1个 - 工厂配置文件 (factories): 1个 - YAML配置文件 (.yml):...
v0.4 - MyBatis数据库的配置单独放在一个properties文件中 v0.5 - 为实体类定义别名,简化sql映射xml文件中的引用 v0.6 - 解决字段名与实体类属性名不相同的冲突 v0.7 - MyBatis中使用association标签来解决一对一的...
-- redis 配置,也可以把配置挪到properties配置文件中,再读取 --> <!-- 这种 arguments 构造的方式,之前配置有缺点。 这里之前的配置有问题,因为参数类型不一致,有时候jar和环境的问题,...
配置Spring和MyBatis:根据实际情况修改mybatis-config.xml和spring-mvc.xml文件中的配置信息,以便系统能够正确读取数据库表结构和执行SQL语句。启动项目:在命令行中进入项目根目录,执行./mvnw spring-boot:run...
3.1 应用场景 在一些开源框架里,如Spring,Struts,Hibernate,MyBatis等,应用程序会提供一个配置文件如xml文件或者properties,然后在Java类里面解析xml或properties里面的内容,得到一个字符串,然后用反射机制...
读取Properties 项目源码 Spring Data JPA 的使用 JPA简单概念 快速上手 基本查询 复杂查询 关联查询 项目源码 模板引擎Thymeleaf 简单上手 常用标签汇总 项目源码 JPA 和 Thymeleaf 实践 多环境配置...
Spring+SpringMVC+Mybatis框架集成公共模块,包括公共配置、MybatisGenerator扩展插件、通用BaseService、工具类等。 > zheng-admin 基于bootstrap实现的响应式Material Design风格的通用后台管理系统,`zheng`...
upload.properties 如属性文件所描述,如图片想放到项目中,res.upload.url注释即可 7、缓存设置。为兼容jdk1.7,此版本中暂时废弃j2cache,默认仅使用ehcache缓存,如想使用,修改J2CacheUtil,用CacheJ2Utils替换...
由(jeesuite-admin)输出兼容遗留kafka系统、支持发送和接收无封装的消息mybatis模块代码生成、自动CRUD、可无缝对接mybaits增强框架Mapper基于properties配置多数据源支持,无需修改XML读写分离,事务内操作强制读...