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