错误信息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in class path resource [org/springframework/boot/autoconfigure/jdbc/XADataSourceAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is java.lang.IllegalStateException: Unable to create XADataSource instance from ‘com.mysql.jdbc.jdbc2.optional.MysqlXADataSource’
分析原因
分析代码得知:
当 getXa().getDataSourceClassName()
为空时,XADataSource
将从url
(连接串)中分析出DataSourceClassName,com.mysql.jdbc.jdbc2.optional.MysqlXADataSource,而这个 又不存在,而报错!
解决方法
在配置中显示指定:
com.mysql.cj.jdbc.MysqlXADataSource
com.alibaba.druid.pool.xa.DruidXADataSource
xa:
dataSourceClassName: com.mysql.cj.jdbc.MysqlXADataSource