单独使用druid-spring-boot-starter产生的bug

bug

java 复制代码
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-12-29 15:10:08.697 ERROR 14860 --- [           main] o.s.boot.SpringApplication               : Application run failed

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
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:713) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:693) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:408) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:921) ~[spring-context-5.3.30.jar:5.3.30]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.30.jar:5.3.30]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.16.jar:2.7.16]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) [spring-boot-2.7.16.jar:2.7.16]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.16.jar:2.7.16]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.7.16.jar:2.7.16]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.7.16.jar:2.7.16]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.7.16.jar:2.7.16]
	at com.knife.Application.main(Application.java:11) [classes/:na]
Caused by: 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
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:710) ~[spring-beans-5.3.30.jar:5.3.30]
	... 20 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType
	at org.springframework.boot.jdbc.EmbeddedDatabaseConnection.<clinit>(EmbeddedDatabaseConnection.java:55) ~[spring-boot-2.7.16.jar:2.7.16]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.afterPropertiesSet(DataSourceProperties.java:113) ~[spring-boot-autoconfigure-2.7.16.jar:2.7.16]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.30.jar:5.3.30]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.30.jar:5.3.30]
	... 30 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType
	at java.net.URLClassLoader.findClass(URLClassLoader.java:387) ~[na:1.8.0_341]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_341]
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_341]
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_341]
	... 34 common frames omitted

仔细看,可以发现,异常的原因 是:找不到 org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType

根据这个信息可以推测出是少了 springframework.jdbc 的 jar 包,所有我们在 pom.xml 添加依赖

产生这个bug的原因:

  1. 单独使用druid-spring-boot-starter依赖,没有使用其他相关的依赖(这里的原因是因为缺少了springframework.jdbc 的 jar 包),例如数据库驱动程序和Spring Boot的JPA依赖,以便与数据库进行交互。

对druid-spring-boot-starter的解析

  1. druid-spring-boot-starter是一个用于集成Druid连接池 的Spring Boot Starter项目。它提供了一些自动配置和默认值,使得在Spring Boot应用中使用Druid连接池变得更加方便。然而,druid-spring-boot-starter本身并不能单独使用,它需要与其他相关的依赖一起使用才能发挥作用。

druid-spring-boot-starter的使用

  1. 在使用druid-spring-boot-starter之前,你需要在你的Spring Boot项目中添加以下依赖:
xml 复制代码
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <!-- 版本需要根据实际情况-->
    <version>1.1.9</version>
</dependency>

这样,当你启动你的Spring Boot应用时,druid-spring-boot-starter会自动配置Druid连接池,并将其作为数据源提供给你的应用程序。

也可以在配置文件里面显式配置数据源:

(方式一)

yml 复制代码
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis?serverTimeZone=UTC
    username: root
    password: dadao
    type: com.alibaba.druid.pool.DruidDataSource     #指定数据源类型

# serverTimeZone=UTC      这个是配置时区的

(方式二:springboot整合数据源的话,这个比较正规,推荐使用这种方式)

yml 复制代码
spring:
  datasource:
    druid:   # 这个druid就是指定使用的数据源了
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/mybatis?serverTimeZone=UTC
      username: root
      password: dadao
  1. 除了druid-spring-boot-starter之外,你还需要在你的项目中添加其他相关的依赖 ,例如数据库驱动程序和Spring Boot的JPA依赖,以便与数据库进行交互。
    • 一般都会使用jdbc的依赖与其搭配。
      • 比如:
xml 复制代码
<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
</dependency>

在springboot项目中一般使用内部集成的spring-boot-starter-jdbc依赖,因为其里面也包含了spring-jdbc。

或者使用mybatis与druid搭配,mybatis依赖里面也集成了spring-boot-starter-jdbc依赖

xml 复制代码
<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.3.1</version>
</dependency>


或者使用mybatis-plus与druid搭配,mybatis-plus依赖里面也集成了spring-boot-starter-jdbc依赖:

xml 复制代码
<dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3</version>
</dependency>

类似的其他Spring Boot的JPA依赖也行。

总结:

总结来说,druid-spring-boot-starter不能单独使用,它需要与其他相关的依赖一起使用才能实现Druid连接池的功能。

相关推荐
有梦想的攻城狮1 小时前
spring-cloud-alibaba-nacos-config使用说明
java·spring·nacos·springcloud·配置中心
呆萌很2 小时前
SpringBoot+MyBatis Plus+PageHelper+vue+mysql 实现用户信息增删改查功能
spring boot
Yan-英杰3 小时前
【百日精通JAVA | SQL篇 | 第三篇】 MYSQL增删改查
java·数据库·sql
矛取矛求4 小时前
C++ 标准库参考手册深度解析
java·开发语言·c++
cijiancao4 小时前
23 种设计模式中的解释器模式
java·设计模式·解释器模式
南七行者4 小时前
对模板方法模式的理解
java·设计模式·模板方法
麻芝汤圆5 小时前
MapReduce 的广泛应用:从数据处理到智能决策
java·开发语言·前端·hadoop·后端·servlet·mapreduce
努力的搬砖人.5 小时前
java如何实现一个秒杀系统(原理)
java·经验分享·后端·面试
哈哈哈哈哈哈哈哈哈...........5 小时前
【java】在 Java 中,获取一个类的`Class`对象有多种方式
java·开发语言·python
fallwind_of_july5 小时前
java项目分享-分布式电商项目附软件链接
java·redis·分布式·mongodb·elasticsearch·微服务·rabbitmq