SpringBoot启动报错如下:
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-17 23:50:15.188 ERROR 17340 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
原因
pom.xml里引入了mybatis-spring-boot-starter依赖之后会自动配置默认的DataSource,
由于配置sharding或多数据源, 没有配置spring默认的数据源配置项.
问题解决办法
把spring boot自动初始化DataSource相关的代码禁止掉
解决办法1.在启动类的@SpringBootApplication加上:
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
解决办法2.在application.properties里配置:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
大坑
发现做了以上两种解决办法都没用:
发现启动日志有DruidDataSourceAutoConfigure初始化:
2020-02-17 23:50:15.093 INFO 17340 --- [ main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
分析原因:
引用了druid-spring-boot-starter, 该starter包有它自己的自动配置类DruidDataSourceAutoConfigure, 该类有自己的配置项,
会去在配置文件里面找对应的配置项,而我们也没有配置改项
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
导致报错和mybatis自动配置项一样, 产生了误导.
做了相应的处理却依然报错 头疼不已
解决:
1.使用普通依赖, 非start.
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.9</version>
</dependency>
2.启动类增加
@SpringBootApplication(exclude = { DruidDataSourceAutoConfigure.class,DataSourceAutoConfiguration.class })
3.application.properties增加
spring.autoconfigure.exclude=com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure,org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration
注意
是具体情况看原框架是否兼容了spring的配置和事务
测试事务看看事务是否生效不生效, 说明默认的配置没起作用, 多数据源或者动态数据源需要自己的sqlsessionfactory中 注入事务管理器