问题描述

在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. SQLServr添加数据列

    数据列定义 表中数据行的数据插入和数据类型都是基于数据列的,学会添加数据列在开发过程中是必不可少的. 使用SSMS数据库管理工具添加数据列 在数据表中添加一列或者多列步骤相同 1.连接数据库,选择数据 ...

  2. Maven构建项目出现No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? 你应该 ...

  3. Spark的性能调优杂谈

    下面这些关于Spark的性能调优项,有的是来自官方的,有的是来自别的的工程师,有的则是我自己总结的. 基本概念和原则 <1>  每一台host上面可以并行N个worker,每一个worke ...

  4. 背景图片固定不随页面上下滚动而滚动 ,属性 background-attachment

    <div id="testimonials-section" class="text-center"> </div> css: #tes ...

  5. Navicat Premium 12.0.18安装与激活

    因为要学习测试,postgresql , 没弄过所以用这个软件... https://www.jianshu.com/p/42a33b0dda9c

  6. Java list 转字符串并加入分隔符的方法

    import org.apache.commons.lang.StringUtils; List<String> list=new ArrayList<String>(); l ...

  7. ESP8266远程OTA升级

    https://blog.csdn.net/xh870189248/article/details/80095139 https://www.wandianshenme.com/play/arduin ...

  8. JVM内存溢出时快照转存HeapDump到文件

    # Heap Dump 获取方式使用 JVM 参数获取 dump 文件进入Tomcat的'bin'目录,在'catalina.sh'文件里添加如下内容 >-XX:+HeapDumpOnOutOf ...

  9. mac 版 Pycharm 激活

    mac 版 Pycharm 激活   获取注册码地址: http://idea.lanyus.com 输入注册码之后可能会报 this license XXXXXXXX has been cancel ...

  10. Educational Codeforces Round 62 (Rated for Div. 2) - C Playlist

    当时题意看错了...不过大致思路是对的,唯一没有想到的就是用优先队列搞这个东西,真是不该啊... 题意大概就是,有N首歌,N首歌有两个东西,一个是长度Ti,一个是美丽值Bi,你最多可以选择K首歌, 这 ...