spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题
来源:互联网 发布:操盘手训练软件 编辑:程序博客网 时间:2024/05/17 05:12
spring里使用org.mybatis.spring.mapper.MapperScannerConfigurer 进行自动扫描的时候,设置了sqlSessionFactory 的话,可能会导致PropertyPlaceholderConfigurer失效,也就是用${jdbc.username}这样之类的表达式,将无法获取到properties文件里的内容。 导致这一原因是因为,MapperScannerConigurer实际是在解析加载bean定义阶段的,这个时候要是设置sqlSessionFactory的话,会导致提前初始化一些类,这个时候,PropertyPlaceholderConfigurer还没来得及替换定义中的变量,导致把表达式当作字符串复制了。 但如果不设置sqlSessionFactory 属性的话,就必须要保证sessionFactory在spring中名称一定要是sqlSessionFactory ,否则就无法自动注入。
问题解决:配置文件如下(主要改动了MapperScannerConfigurer的配置,使用sqlSessionFactoryBeanName进行延迟注入)
applicationContext.xml
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!-- 配置 将jdbc的连接参数使用外部properties--> <bean class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties"/> </bean> <!-- 配置c3p0连接池 --> <bean id="comboPooledDataSourceID" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driverClassName}"/> <property name="jdbcUrl" value="${url}"/> <property name="user" value="${username}"/> <property name="password" value="${password}"/> <property name="maxPoolSize" value="${maxPoolSize}"/> <property name="minPoolSize" value="${minPoolSize}"/> <property name="checkoutTimeout" value="${checkoutTimeout}"/> <property name="acquireRetryAttempts" value="${acquireRetryAttempts}"/> </bean> <!-- 加载mybatis配置文件和映射文件,替换MyBatisUtil工具类 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="mapperLocations" value="classpath:com/ssm/dao/*.xml"/> <property name="dataSource" ref="comboPooledDataSourceID"/> </bean> <!-- 配置 转换器,对于在basePackage设置的包(包括子包)下的接口类,如果接口类的全类名在Mapper.xml文件中和定义过命名空间一致, 将被转换成spring的BEAN,在调用的地方通过@Autowired方式将可以注入接口实例 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" ><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /><property name="basePackage" value="com.ssm.dao" /></bean> </beans>
jdbc.properties
#jdbc驱动driverClassName=com.mysql.jdbc.Driver#数据库连接地址url=jdbc:mysql://localhost:3306/employeedb#用户名username=root#密码password=123456#连接池最大连接数maxPoolSize=30#连接池最小连接数minPoolSize=10#连接超时时间checkoutTimeout=1000#当获取连接失败时重试次数acquireRetryAttempts=2
0 0
- spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题
- spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题处理方法
- mybatis整合spring使用MapperScannerConfigurer配置问题
- mybatis-spring:MapperScannerConfigurer、MapperScannerConfigurer
- 关于Spring整合mybatis时配置MapperScannerConfigurer的问题
- Spring PropertyPlaceholderConfigurer的使用
- Spring PropertyPlaceholderConfigurer的使用
- spring propertyplaceholderconfigurer的使用
- Spring的PropertyPlaceholderConfigurer使用
- Spring:PropertyPlaceholderConfigurer的使用
- MapperScannerConfigurer在mybatis-spring的妙用
- MapperScannerConfigurer在mybatis-spring的妙用
- 解决“Spring 使用MapperScannerConfigurer无法读取*.properties文件"的问题
- spring-mybatis整合-MapperScannerConfigurer
- 【spring+mybatis】MapperScannerConfigurer
- Spring-MyBatis 之 MapperScannerConfigurer
- spring mybatis 之MapperScannerConfigurer
- 【spring+mybatis】MapperScannerConfigurer
- int、long、long long取值范围 实例: 判断是否超出整形范围
- ubuntu14.04 配置VNC服务,亲测可用
- 练手python_随机生成200个获奖码
- iOS调用WebService(SOAP接口)
- Groory(一) 语法篇---极速入门总结
- spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题
- 【GDB调试学习笔记】 Makefie上
- Hilari 中文API说明
- HDU 1811 Rank of Tetris(拓扑排序,队列实现,并查集)
- Spark ML包随机森林回归
- Opencv图像识别从零到精通(22)-----hough变换检测直线与圆
- Java Web开发-简单的Java Web项目(含访问本地MySQL)-应用例程
- hdu2204 Eddy's 爱好 【容斥原理】
- distinct 和 group by 在查询多列数据去重复时的区别在哪?