日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

解決SpringBoot啟動報錯 :o.s.b.d.LoggingFailureAnalysisReporter

作者:峰子的番茄肥牛 更新時間: 2024-03-22 編程語言

目錄

參考信息來源:? ??

報錯代碼、信息如下:

報錯原因

解決辦法一般分兩種情況

第一種情況:項目不需要連接數據庫,啟動時報錯

第二種情況:項目需要連接數據庫,啟動時報錯

解決方案①:在配置文件中沒有添加數據庫配置信息,則需要編寫相應的配置

解決方案②:項目沒有加載到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 datasourcehttps://blog.csdn.net/renkai721/article/details/112257894springboot項目 o.s.b.d.LoggingFailureAnalysisReporter 錯誤解決方法https://blog.csdn.net/weixin_39872341/article/details/105419983com.mysql.jdbc.Driver 和 com.mysql.cj.jdbc.Driver 的區別https://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

<packaging>jar</packaging>

如果pom中指定使用jar,系統不會自動讀取到yml或者properties文件的,需要我們手動配置pom.xml。

<!--build放在</dependencies>標簽的后面,主要加入的是resources標簽 -->
<!--resources標簽可以告訴系統啟動的時候能夠讀取到這些后綴的文件 -->
<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.yml</include>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>lib</directory>
                <includes>
                    <include>**/*.jar</include>
                </includes>
            </resource>
        </resources>
    </build>
解決方案③:項目使用 Profile多環境支持,但未加載到正確的配置文件

默認情況下,spring boot使用application.properties配置文件

解決辦法:手動指定profile(4種方式)

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

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

properties示例:

spring.proflies.active=dev

yml示例(yml支持多文檔塊方式):

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

方式三:控制臺指定(將項目打包后在控制臺運行時調用指令)

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

?方式四:虛擬機參數指定

-Dspring.profiles.active=dev

解決方案④:項目使用了springcloud+nacos系列

????????啟動項目時候需要手動指定【--spring.profiles.active=test】,那么在resources文件夾下就必須要有bootstrap-test.yml或者application-test.yml文件,同時配置文件中連接的nacos地址里面也必須配置對應的命名空間,和對應服務名稱的yml文件,否則也是報錯。下面是配置文件的截圖

?

????????當然,這只是解決一部分報錯的方案,具體解決辦法還需參考項目Description提供的報錯信息。例如端口號被占用時也會報出o.s.b.d.LoggingFailureAnalysisReporter的錯誤信息,這時候就需要更改端口號,方法也很多,就比如在配置文件中指定server.port=8081(默認值8080)等。

原文鏈接:https://blog.csdn.net/weixin_63453779/article/details/131506225

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新