spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题处理方法
来源:互联网 发布:人工智能答案 编辑:程序博客网 时间:2024/04/29 23:21
问题描述:
在spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,他会优先于PropertyPlaceholderConfigurer执行,从而导致PropertyPlaceholderConfigurer失效,这时在xml中用${url}、${username}、${password}等这样之类的表达式,将无法获取到properties文件里的内容。
处理方法:
<!-- 配置mybatis --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml"></property><!-- mapper扫描 --><property name="mapperLocations" value="classpath*:com/xx/**/mapper/*Mapper.xml" /></bean><!-- 映射Mapper文件 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.xx.**.dao"></property><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property></bean>
修改为:
<!-- 配置mybatis --><bean id="mybatisSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="configLocation" value="classpath:mybatis-config.xml"></property><!-- mapper扫描 --><property name="mapperLocations" value="classpath*:com/xx/**/mapper/*Mapper.xml" /></bean><!-- 映射Mapper文件 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.xx.**.dao"></property><property name="sqlSessionFactoryBeanName" value="mybatisSqlSessionFactory"></property></bean>
问题分析:
使用sqlSessionFactoryBeanName注入,不会立即初始化sqlSessionFactory, 所以不会引发提前初始化问题,同时还应注意在配置org.mybatis.spring.SqlSessionFactoryBean这个Bean时,id不能为sqlSessionFactory,如果为这样的话会导致MapperScannerConigurer在bean定义加载时,加载PropertyPlaceholderConfigurer还没来得及替换定义中的变量
0 0
- spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题处理方法
- spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题
- mybatis整合spring使用MapperScannerConfigurer配置问题
- mybatis-spring:MapperScannerConfigurer、MapperScannerConfigurer
- MapperScannerConfigurer方法整合spring+mybatis
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- Spring与Mybatis整合的MapperScannerConfigurer处理过程
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- Spring与Mybatis整合的MapperScannerConfigurer处理过程
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- Spring与Mybatis整合的MapperScannerConfigurer处理过程源码分析
- 关于Spring整合mybatis时配置MapperScannerConfigurer的问题
- Android 自定义View——使用画布canvas
- 【Egret】里使用video标签
- 数据结构——栈
- java反射详解
- 三个月从入门成为前端开发工程师系列视频教程
- spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题处理方法
- Tomcat下CAS Server证书配置
- Linux 运行进程实时监控pidstat命令详解
- webMagic学习笔记
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
- 算法优秀书籍汇总---学习路线图
- android 电源管理
- NodeMCU文档中文翻译 2 首页
- 关于keystore的简单介绍