有时在新建或拉他人的springboot项目时,启动会报这样的错
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name
可能堆栈信息如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSource': Unsatisfied dependency expressed through field 'basicProperties'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jdbc.DataSourceProperties]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jdbc.DataSourceProperties]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
..
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.autoconfigure.jdbc.DataSourceProperties]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
...
Caused by: java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException
根本问题所在是Springboot自动配置时,发生了配置依赖相关性错误,导致无法创建bean对象。所以排查首先要找到这个bean对象。再追其失败原因.
比如上面这里这个异常是无法创建dataSource这个bean对象,只需要排查跟数据源创建相关的问题就行了。
经常发生在新建springboot或启动他人的项目时,解决方案就围绕着数据连接相关去排查。
-
检查application.yml是否配置了 spring.datasource 节点。缺少会导致自动DataSourceAutoConfiguration,DataSourceProperties 自动装配失败。
-
如果项目中不使用数据库连接,在SpringBootApplication注解上把DataSourceAutoConfiguration这个自动装配排除掉.
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
- 检查maven是否依赖了如druid-spring-boot-starter等三方数据库连接池依赖,可以选择从maven中排除依赖或在SpringBootApplication注解上排除。
<dependency>
<groupId>com.kuizii.framework</groupId>
<artifactId>com-kuizii-base-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,DruidDataSourceAutoConfigure.class})