spring-boot-autoconfigure误引入spring-boot-starter-data-jpa而导致数据源初始化异常
2024-01-02 16:18:06
一、现状描述
某个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 class
2024-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 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 (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的数据源就配置啊。
这就非常提倡,为了一双玉的筷子,反过来去买一个金碗,再进而去买银碗篮。。。
文章来源:https://blog.csdn.net/zhuganlai168/article/details/135340312
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!