解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporter

2024-04-10 1625阅读

温馨提示:这篇文章已超过387天没有更新,请注意相关的内容是否还可用!

目录

参考信息来源:    

报错代码、信息如下:

报错原因

解决办法一般分两种情况

第一种情况:项目不需要连接数据库,启动时报错

第二种情况:项目需要连接数据库,启动时报错

解决方案①:在配置文件中没有添加数据库配置信息,则需要编写相应的配置

解决方案②:项目没有加载到yml或者properties文件,特别是自己的pom打包是jar的项目,需要查看自己的pom.xml文件中的packaging

解决方案③:项目使用 Profile多环境支持,但未加载到正确的配置文件

方式一:在配置文件中指定spring.profiles.active={profile}

 方式二:命令行指定 --spring.profiles.active=dev

方式三:控制台指定(将项目打包后在控制台运行时调用指令)

 方式四:虚拟机参数指定

解决方案④:项目使用了springcloud+nacos系列


遇到这个问题时查了许多博主的信息,此处仅为个人方面的总结(汇总但并不全面)

参考信息来源:    

彻底解决Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporterhttps://blog.csdn.net/renkai721/article/details/112257894springboot项目 o.s.b.d.LoggingFailureAnalysisReporter 错误解决方法解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporterhttps://blog.csdn.net/weixin_39872341/article/details/105419983com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver 的区别解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporterhttps://blog.csdn.net/a907691592/article/details/96876030

报错代码、信息如下:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2023-07-02 22:02:05.181 ERROR 116952 --- [           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 (no profiles are currently active).
Process finished with exit code 1

错误信息翻译:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.       

(配置数据源失败:“url”属性未指定,无法配置嵌入数据源)

Reason: Failed to determine a suitable driver class       

(原因:无法确定合适的驱动程序类)

报错原因

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

解决办法一般分两种情况

第一种情况:项目不需要连接数据库,启动时报错

解决办法:在配置类注解@SpringBootApplication 后面加上 (exclude ={DataSourceAutoConfiguration.class}) 即可。

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//@SpringBootApplication
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}):
        这个注解的作用是:排除自动注入数据源的配置(即取消数据库的配置)。 
        DataSourceAutoConfiguration.class会自动查找配置文件(application.properties或者
application.yml)中的相关数据源配置信息。
        另外,DataSourceAutoConfiguration.class默认会自动配置单数据源,如果想在项目中使用多数据源就需要排除(exlcude)它,再手动指定多数据源。

第二种情况:项目需要连接数据库,启动时报错

解决方案①:在配置文件中没有添加数据库配置信息,则需要编写相应的配置

yml配置示例:

注意,yml配置文件中在设置值时,冒号后面要加上一个空格,这是yml的基本语法。

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

properties配置示例:

spring.datasource.url=jdbc:mysql://localhost:3306/test?setUnicode=true&characterEncoding=utf8
spring.datasource.name=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

另外,由于mysql的版本不同,相应的 ur l和 driver-class-name 设置的值也有所不同,具体参考

com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver 的区别

解决方案②:项目没有加载到yml或者properties文件,特别是自己的pom打包是jar的项目,需要查看自己的pom.xml文件中的packaging

jar

如果pom中指定使用jar,系统不会自动读取到yml或者properties文件的,需要我们手动配置pom.xml。



        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
        
            
                src/main/java
                
                    **/*.yml
                    **/*.properties
                    **/*.xml
                
                false
            
            
                src/main/resources
                
                    **/*.yml
                    **/*.properties
                    **/*.xml
                
                false
            
            
                lib
                
                    **/*.jar
                
            
        
    
解决方案③:项目使用 Profile多环境支持,但未加载到正确的配置文件

默认情况下,spring boot使用application.properties配置文件

解决办法:手动指定profile(4种方式)

以当前需要指定的配置文件名为 application-dev.properties 为例

方式一:在配置文件中指定spring.profiles.active={profile}

properties示例:

spring.proflies.active=dev

yml示例(yml支持多文档块方式):

解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporter

 方式二:命令行指定 --spring.profiles.active=dev

解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporter

方式三:控制台指定(将项目打包后在控制台运行时调用指令)

java -jar jar包名 --spring.profiles.active=dev

解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporter

解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporter

 方式四:虚拟机参数指定

-Dspring.profiles.active=dev

解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporter

解决方案④:项目使用了springcloud+nacos系列

        启动项目时候需要手动指定【--spring.profiles.active=test】,那么在resources文件夹下就必须要有bootstrap-test.yml或者application-test.yml文件,同时配置文件中连接的nacos地址里面也必须配置对应的命名空间,和对应服务名称的yml文件,否则也是报错。下面是配置文件的截图

解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporter

解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporter 解决SpringBoot启动报错 :o.s.b.d.LoggingFailureAnalysisReporter

        当然,这只是解决一部分报错的方案,具体解决办法还需参考项目Description提供的报错信息。例如端口号被占用时也会报出o.s.b.d.LoggingFailureAnalysisReporter的错误信息,这时候就需要更改端口号,方法也很多,就比如在配置文件中指定server.port=8081(默认值8080)等。

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]