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配置信息全都未解析。
  我本来的配置文件如下:


	

	
	
		
		
		
		
	

	
	
		
		
		
		
		
		
	

	
	
		
		
		
	

	
	
		
	

	
	



  修改后的配置文件如下:


	

	
	
		
		
		
		
	

	
	
		
		
		
		
		
		
	

	
	
		
		
		
	

	
	
		
	

	
	



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

你可能感兴趣的:(JAVA,SSM)