问题描述

在web项目中同时集成了spring mvc和mybatis。

将jdbc配置参数独立在外部配置文件中,然后通过<context:property-placeholder>引入。

此时在Spring中注入org.mybatis.spring.mapper.MapperScannerConfigurer,如下所示:

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.chench.test.springmvc.dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>

如果直接配置属性sqlSessionFactory,并设置为指定的sqlSessionFactory对象,那么在启动spring时会报错:

Caused by: java.lang.NumberFormatException: For input string: "${master.acquireIncrement}"

数据源配置中无法正确引用外部文件中配置的jdbc参数。

必须修改为配置属性sqlSessionFactoryBeanName,才能正确引用到对应的jdbc配置参数。

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.chench.test.springmvc.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

这是什么原因呢?

原因分析及解决方案

实际上,这是MyBatis早期版本的一个BUG,详见:https://github.com/mybatis/old-google-code-issues/issues/414

而且,在最新版本的MyBatis中,同样不再推荐使用设置Bean属性的方式,而是通过设置Value属性。

  /**
* Specifies which {@code SqlSessionFactory} to use in the case that there is
* more than one in the spring context. Usually this is only needed when you
* have more than one datasource.
* <p>
* @deprecated Use {@link #setSqlSessionFactoryBeanName(String)} instead.
*
* @param sqlSessionFactory
*/
// 这是一个声明为废弃的方法,不推荐使用
@Deprecated
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
} /**
* Specifies which {@code SqlSessionFactory} to use in the case that there is
* more than one in the spring context. Usually this is only needed when you
* have more than one datasource.
* <p>
* Note bean names are used, not bean references. This is because the scanner
* loads early during the start process and it is too early to build mybatis
* object instances.
*
* @since 1.1.0
*
* @param sqlSessionFactoryName Bean name of the {@code SqlSessionFactory}
*/
// 解释了为什么应该使用设置Value的方式而不是引用Bean的原因
public void setSqlSessionFactoryBeanName(String sqlSessionFactoryName) {
this.sqlSessionFactoryBeanName = sqlSessionFactoryName;
}

实际上,通常情况下无需手动为MapperScannerConfigurer注册SqlSessionFactory,这个工作由Spring框架完成,因为MapperScannerConfigurer已经实现了接口BeanDefinitionRegistryPostProcessor。

public class MapperScannerConfigurer implements BeanDefinitionRegistryPostProcessor, InitializingBean, ApplicationContextAware, BeanNameAware {
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) {
if (this.processPropertyPlaceHolders) {
processPropertyPlaceHolders();
} ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry);
scanner.setAddToConfig(this.addToConfig);
scanner.setAnnotationClass(this.annotationClass);
scanner.setMarkerInterface(this.markerInterface);
// 由Spring框架自动注入SqlSessionFactory
scanner.setSqlSessionFactory(this.sqlSessionFactory);
scanner.setSqlSessionTemplate(this.sqlSessionTemplate);
// 由Spring框架自注入sqlSessionFactoryBeanName
scanner.setSqlSessionFactoryBeanName(this.sqlSessionFactoryBeanName);
scanner.setSqlSessionTemplateBeanName(this.sqlSessionTemplateBeanName);
scanner.setResourceLoader(this.applicationContext);
scanner.setBeanNameGenerator(this.nameGenerator);
scanner.registerFilters();
scanner.scan(StringUtils.tokenizeToStringArray(this.basePackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS));
}
}

MyBatis集成到Spring时配置MapperScannerConfigurer出错的更多相关文章

  1. Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置

    Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...

  2. Mybatis集成到spring boot

    1, Mybatis简介 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可 ...

  3. Spring Boot 2 实践记录之 MyBatis 集成的启动时警告信息问题

    按笔者 Spring Boot 2 实践记录之 MySQL + MyBatis 配置 中的方式,如果想正确运行,需要在 Mapper 类上添加 @Mapper 注解. 但是加入此注解之后,启动时会出现 ...

  4. mybatis集成到spring理解

  5. 在 JPA、Hibernate 和 Spring 中配置 Ehcache 缓存

    jpa, hibernate 和 spring 时配置 ehcache 二级缓存的步骤. 缓存配置 首先在 persistence.xml 配置文件中添加下面内容: <property name ...

  6. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  7. MyBatis 学习-与 Spring 集成篇

    根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...

  8. 深入浅出mybatis之与spring集成

    目录 写在前面 详细配置 1.dataSource(数据源) 2.sqlSessionFactory(Session工厂) 3.Mapper(映射器) 4.TransactionManager(事务管 ...

  9. Spring,Struts2,MyBatis,Activiti,Maven,H2,Tomcat集成(三)——H2,MyBatis集成

    1.配置h2,连接池,MyBatis Maven依赖: <!-- spring与数据库访问集成(非Hibernate) --> <dependency> <groupId ...

随机推荐

  1. centosFTP服务搭建及权限配置

    引用一个其他大佬对vsftpd的描述: vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点. vsftpd 是一个 UNIX 类操作系统上运行的服务器的 ...

  2. 【Python 03】程序设计与Python语言概述

    人生苦短,我用Python. Python在1990年诞生于荷兰,2010年Python2发布最后一版2.7,Python核心团队计划在2020年停止支持 Python2,目前Python3是未来. ...

  3. python项目在无外网的生产环境解决沙盒依赖问题

    参考 https://yq.aliyun.com/articles/159599 https://www.jianshu.com/p/08c657bd34f1 缺点是 只能针对python的环境 做沙 ...

  4. day 10函数二

    今日内容 '''实参:调用函数,在括号内传入的实际值,值可以为常量.变量.表达式或三者的组合​*****形参:定义函数,在括号内声明的变量名,用来接受外界传来的值​'''​'''注:形参随着函数的调用 ...

  5. pd.read_csv() 、to_csv() 之 常用参数

    本文简单介绍一下read_csv()和 to_csv()的参数,最常用的拿出来讲,较少用的请转到官方文档看. 一.pd.read_csv() 作用:将csv文件读入并转化为数据框形式. pd.read ...

  6. PHP性能优化:in_array和isset 在大数组查询中耗时相差巨大,以及巧妙使用array_flip

    今天在PHP业务开发中,发现了一个问题. 两个较大数组(20万+元素),遍历其中一个$a,另一个数组$b用于查找元素. 比如 foreach($a as $val){ if(in_array($xx, ...

  7. 在Bootstrap开发框架的前端视图中使用@RenderPage实现页面内容模块化的隔离,减少复杂度

    在很多开发的场景中,很多情况下我们需要考虑抽象.以及模块化等方面的内容,其目的就是为了使得开发的时候关注的变化内容更加少一些,整体开发更加简单化,从而减少开发的复杂度,在Winform开发的时候,往往 ...

  8. python json库序列化支持中文

    import json d = {"name":"英雄无敌7"} res = json.dumps(d) # 打印res 会显示 {"name&quo ...

  9. python print 在windows上 出现 Bad file descriptor error

    先说一下情况,一个python写的采集程序,做成windows服务在windows上运行. 这个问题出现的挺奇特,本来一套采集程序,一个采集文件的时候没问题,两个采集文件的时候也没问题,当三个采集文件 ...

  10. pyspider煎蛋无聊图爬取

    命令行pyspider,启动pyspider. web预览界面太小,解决方法:找到pyspider的安装路径下的debug.min.css,修改css代码: 将其中的iframe{border-wid ...