Spring+MyBatis加载properties文件设置数据源失败原因及解决方案

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初始化完成后,再构造该类。

转载于:https://my.oschina.net/enjoymore/blog/473137

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值