網站首頁 編程語言 正文
目錄
參考信息來源:? ??
報錯代碼、信息如下:
報錯原因
解決辦法一般分兩種情況
第一種情況:項目不需要連接數據庫,啟動時報錯
第二種情況:項目需要連接數據庫,啟動時報錯
解決方案①:在配置文件中沒有添加數據庫配置信息,則需要編寫相應的配置
解決方案②:項目沒有加載到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
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-22 詳解C#對Dictionary內容的通用操作_C#教程
- 2022-09-24 一文詳解go?mod依賴管理詳情_Golang
- 2022-05-11 python?DataFrame數據格式化(設置小數位數,百分比,千分位分隔符)_python
- 2022-05-29 在Linux系統上安裝PostgreSQL數據庫_PostgreSQL
- 2022-04-11 【Android,kotlin】寫倒計時CountDown的正確姿勢
- 2022-09-06 C++詳細講解引用類型_C 語言
- 2022-11-14 mq消息積壓怎么對應
- 2022-09-07 在SQL?Server中使用?Try?Catch?處理異常的示例詳解_MsSql
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支