java.lang.IllegalStateException: Could not load JDBC driver class [${jdbc.driver}]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.mybatis.spring.mapper.MapperScannerConfigurer#0' defined in file [xxx\applicationContext-mybatis.xml]: Cannot resolve reference to bean 'sqlSessionFactory' while setting bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in file [xxx\applicationContext-mybatis.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in file [xxx\applicationContext-mybatis.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${jdbc.driver}]
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1469)

  首先,我把异常定位到Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [${jdbc.driver}]信息上,这个异常我一开始一直以为${jdbc.driver}的值是获取到了,可能是驱动jar包之类的原因导致jdbc没加载成功,所以我查了下项目的Libraries,发现里面有mysql-connector-java的依赖。然后,我就将driverClassName的值改为com.mysql.jdbc.Driver,项目部署没有报错。
  可是,当我使用mapper操作数据库时,又出现了错误,报错信息如下:

Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for ${jdbc.url}] with root cause
 java.sql.SQLException: No suitable driver found for ${jdbc.url}

  此时,我感觉这${jdbc.url}这些读取jdbc.properties文件的属性的值好像都有问题。经过查阅资料,终于在一篇文章上找到了问题在哪。
引用链接:https://blog.csdn.net/zcl1199/article/details/51172420

  原来这些报错信息是指将${jdbc.driver}这些值当做了字符串,所以报错信息里直接写的${jdbc.driver}而不是解析之后的值。

解决方法

  通过以上资料我们知道了是由于sqlSessionFactory的加载顺序在PropertyPlaceholderConfigurer之前,导致了sqlSessionFactory使用的jdbc配置信息全都未解析。
  我本来的配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!-- 1、数据源 DriverManagerDataSource -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 2.mybatis的SqlSession的工厂:SqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 定义数据源 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- 别名包 -->
		<property name="typeAliasesPackage" value="com.manager.entity"/>
		<!-- mapper.xml -->
		<property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/>
	</bean>

	<!-- 3.mybatis自动扫描加载sql映射文件:MapperScannerConfigurer -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描mapper包 -->
		<property name="basePackage" value="com.manager.mapper"/>
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
	</bean>

	<!-- 4.事物管理:DataSourceTransactionManager -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

	<!-- 5.使用声明事物 -->
	<tx:annotation-driven transaction-manager="txManager"/>

</beans>

  修改后的配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	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
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!-- 1、数据源 DriverManagerDataSource -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- 2.mybatis的SqlSession的工厂:SqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 定义数据源 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- 别名包 -->
		<property name="typeAliasesPackage" value="com.manager.entity"/>
		<!-- mapper.xml -->
		<property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/>
	</bean>

	<!-- 3.mybatis自动扫描加载sql映射文件:MapperScannerConfigurer -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描mapper包 -->
		<property name="basePackage" value="com.manager.mapper"/>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>

	<!-- 4.事物管理:DataSourceTransactionManager -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>

	<!-- 5.使用声明事物 -->
	<tx:annotation-driven transaction-manager="txManager"/>

</beans>

  改动就是将sqlSessionFactory注入改为sqlSessionFactoryBeanName注入,注意要将后面的ref属性改为value。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章