Bootstrap

【已解决】Error creating bean with name ‘dataSource‘: Unsatisfied dependency expressed through field

【已解决】Error creating bean with name 'dataSource': Unsatisfied dependency expressed through field 'basicProperties';

1. 报错

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': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType

2. 原因

spring boot 会默认加载 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 类,DataSourceAutoConfiguration 类使用了 @Configuration 注解向 spring 注入了 dataSource bean
因为工程中没有关于 dataSource 相关的配置信息,当 spring 创建 dataSource bean 时就会因缺少相关信息而报错。

3. 解决方案

在启动类上添加注解:exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class}
示例如下:

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class })
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

4. 参考链接

原文链接:https://blog.csdn.net/xenos2020/article/details/128850826

;