错误:
Caught exception while allowing TestExecutionListener [org.springframewor[email protected]29ee9faa] to prepare test instance [[email protected]]
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountService' defined in class path resource [bean.xml]: Cannot resolve reference to bean 'accountDao' while setting bean property 'accountDao'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accountDao' defined in class path resource [bean.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 class path resource [bean.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 [com.mysql.cj.jdbc.Driver]
后来才发现是因为配置数据源时,driverClassName属性填错了,我导入的mysql-connector-java版本是5.1.6,其driverClassName属性值应为com.mysql.jdbc.Driver,而我错填成com.mysql.cj.jdbc.Driver 导致了这一系列错误。
最后归结其原因就是 mysql-connector-java版本不同,driverClassName也不会有所不同。
下面附上我的mysql的jar包版本和数据源配置的属性值。