当前位置: 首页 > news >正文

spring-boot-autoconfigure误引入spring-boot-starter-data-jpa而导致数据源初始化异常

一、现状描述

某个Grade类引入了jpa的注解:

import javax.persistence.Column;
import javax.persistence.Embeddable;/*** 年级*/
@Embeddable
public class Grade {@Column(name = "code")private int code;
}

并且pom.xml中引入该jar包:spring-boot-starter-data-jpa

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency>

不出意外,在启动的时候会报错如下:

2024-01-02 14:35:23.242  WARN [xxx-service,,,] 2778322 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'hystrix.stream' contains invalid characters, please migrate to a valid format.
2024-01-02 14:35:24.919  WARN [xxx-service,,,] 2778322 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.2024-01-02 14:35:25.564 ERROR [xxx-service,,,] 2778322 --- [           main] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: 
Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: 
Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: 
Unsatisfied dependency expressed through method 'healthEndpoint' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class2024-01-02 14:35:25.595  WARN [xxx-service,,,] 2778322 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
Disconnected from the target VM, address: '127.0.0.1:35843', transport: 'socket'2024-01-02 14:35:25.625 ERROR [xxx-service,,,] 2778322 --- [           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 classAction: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 (the profiles dev are currently active).

错误在于你没有配置rdb的数据源,默认是com.zaxxer.hikari.HikariDataSource。

可是我整个项目都只依赖Mongodb和redis数据库,并无需使用mysql等关系型数据库。

二、解决办法

1、方法一,忽略数据源的自动加载

在application.java启动类上作处理:

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class XxxApplication {public static void main(String[] args) {SpringApplication.run(DataCenterAppApplication.class, args);}
}

2、方法二,去除数据源jdbc的依赖

		<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><exclusions><exclusion><artifactId>spring-boot-starter-jdbc</artifactId><groupId>org.springframework.boot</groupId></exclusion></exclusions></dependency>

3、方法三,去掉jpa(建议方法)

既然没有使用到关系型数据库,就没必要jpa包了。

这里需要说明的是,文章开头说的Grade类,是不必要那些字段的注解。

估计是从其他项目中将代码copy过来的时候,偷懒做法,没有去掉注解。

三、总结

1、查找mvn依赖关系,定位到Hikari包是被谁引入的。

通过问题的报错,反过去推导。

在这里插入图片描述

2、不要引入不必要的jar包,方法一和二虽然解决了问题,但多少有点画蛇添足的嫌疑。

当然,还有人会说,没配置rdb的数据源就配置啊。

这就非常提倡,为了一双玉的筷子,反过来去买一个金碗,再进而去买银碗篮。。。

http://www.lryc.cn/news/271708.html

相关文章:

  • 工程(十六)——自己数据集跑Fast_livo
  • PostgreSQL数据库的json操作
  • gradio-osprey-demo
  • 从仿写持久层框架到MyBatis核心源码阅读
  • 浏览器常用基本操作之python3+selenium4自动化测试
  • 在MySQL中使用VARCHAR字段进行日期筛选
  • 微信小程序自定义步骤条效果
  • QT的信号与槽
  • Python 为UnityAndroid端自动化接入Tradplus广告SDK
  • Matplotlib基础
  • 上海东海职业技术学院低代码实训平台建设项目竞争性磋商公告
  • c语言之将输入的十进制转换成二进制数并打印原码反码补码
  • 算法题明明的随机数
  • B站不赚钱、“芒果”赚钱难,视频“后浪”火拼跨年夜
  • ajax请求的详细流程+详细示例
  • 这些产品手册制作工具,你都值得收藏
  • 跨账号和同账号的ECS云服务器之间迁移教程
  • python virtualenv 虚拟环境命令
  • 深入理解MySQL索引底层数据结构
  • 使用 Tkinter 制作一个进制转换工具,好用!
  • Final Cut 视频剪辑快速入门,小白上手视频课的制作
  • 分布式定时任务Xxl_Job详细使用手册
  • 【PostgreSQL】表操作-修改表
  • 【Java系列】文件操作详解
  • docker-compose 安装 RocketMq
  • 【心得】PHP反序列化高级利用(phar|session)个人笔记
  • MyBatisPlus之增删改查
  • pytorch03:transforms常见数据增强操作
  • blob文件流前端显示pdf
  • Android 接入第三方数数科技平台