spring3 + mybatis 中,使用:jdbc.properties来动态绑定数据源,发生数据无法载入,为一个字符串的问题。
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://127.0.0.1:3306/student_manager?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
jdbc.mysql.username=root
jdbc.mysql.password=root
applicationContent-dataAccess.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-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.mysql.url}" />
<property name="username" value="${jdbc.mysql.username}" />
<property name="password" value="${jdbc.mysql.password}" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
<property name="basePackage" value="com.mybatis" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 启动Spring事务注解功能 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
运行后发生报错
Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${jdbc.driver}]
表名文件已经正确带入,但是没有读到key,而是变成了字符串
原因是,mybatis配置mapper使用的数据源的时候,使用的是:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
<property name="basePackage" value="com.mybatis" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
其中,sqlSessionFactory 是数据源的工厂类引用,这样初始化mybatis时,属性文件的值还没被替换,就开始构造这个sqlSessionFactory类了,所以导致属性值加载失败
修改办法:
升级spring到3.1.1+版本,升级mybatis-spring到1.1.1+版本,把上述配置改为:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="annotationClass" value="org.springframework.stereotype.Repository" />
<property name="basePackage" value="com.mybatis" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
即sqlSessionFactory属性改为sqlSessionFactoryBeanName,ref的bean直接改为字符串的value, 这样没有直接注入sqlSessionFactory,而是等spring初始化完成后,再构造该类。