網(wǎng)站首頁 編程語言 正文
目錄
參考信息來源:? ??
報錯代碼、信息如下:
報錯原因
解決辦法一般分兩種情況
第一種情況:項目不需要連接數(shù)據(jù)庫,啟動時報錯
第二種情況:項目需要連接數(shù)據(jù)庫,啟動時報錯
解決方案①:在配置文件中沒有添加數(shù)據(jù)庫配置信息,則需要編寫相應的配置
解決方案②:項目沒有加載到y(tǒng)ml或者properties文件,特別是自己的pom打包是jar的項目,需要查看自己的pom.xml文件中的packaging
解決方案③:項目使用 Profile多環(huán)境支持,但未加載到正確的配置文件
方式一:在配置文件中指定spring.profiles.active={profile}
?方式二:命令行指定?--spring.profiles.active=dev
方式三:控制臺指定(將項目打包后在控制臺運行時調(diào)用指令)
?方式四:虛擬機參數(shù)指定
解決方案④:項目使用了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 的區(qū)別
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.? ? ? ?
(配置數(shù)據(jù)源失敗:“url”屬性未指定,無法配置嵌入數(shù)據(jù)源)
Reason: Failed to determine a suitable driver class? ? ? ?
(原因:無法確定合適的驅動程序類)
報錯原因
在項目啟動時,Spring Boot會默認加載org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration 這個類,而DataSourceAutoConfiguration類使用了@Configuration注解向spring注入了dataSource bean。因為工程中沒有關于dataSource相關的配置信息,當spring創(chuàng)建dataSource bean因缺少相關的信息就會報錯。
解決辦法一般分兩種情況
第一種情況:項目不需要連接數(shù)據(jù)庫,啟動時報錯
解決辦法:在配置類注解@SpringBootApplication 后面加上 (exclude ={DataSourceAutoConfiguration.class}) 即可。
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
//@SpringBootApplication
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}): ????????這個注解的作用是:排除自動注入數(shù)據(jù)源的配置(即取消數(shù)據(jù)庫的配置)。 ????????DataSourceAutoConfiguration.class會自動查找配置文件(application.properties或者 application.yml)中的相關數(shù)據(jù)源配置信息。 ????????另外,DataSourceAutoConfiguration.class默認會自動配置單數(shù)據(jù)源,如果想在項目中使用多數(shù)據(jù)源就需要排除(exlcude)它,再手動指定多數(shù)據(jù)源。
第二種情況:項目需要連接數(shù)據(jù)庫,啟動時報錯
解決方案①:在配置文件中沒有添加數(shù)據(jù)庫配置信息,則需要編寫相應的配置
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 的區(qū)別
解決方案②:項目沒有加載到y(tǒng)ml或者properties文件,特別是自己的pom打包是jar的項目,需要查看自己的pom.xml文件中的packaging
<packaging>jar</packaging>
如果pom中指定使用jar,系統(tǒng)不會自動讀取到y(tǒng)ml或者properties文件的,需要我們手動配置pom.xml。
<!--build放在</dependencies>標簽的后面,主要加入的是resources標簽 -->
<!--resources標簽可以告訴系統(tǒng)啟動的時候能夠讀取到這些后綴的文件 -->
<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多環(huán)境支持,但未加載到正確的配置文件
默認情況下,spring boot使用application.properties配置文件
解決辦法:手動指定profile(4種方式)
以當前需要指定的配置文件名為 application-dev.properties 為例
方式一:在配置文件中指定spring.profiles.active={profile}
properties示例:
spring.proflies.active=dev
yml示例(yml支持多文檔塊方式):
?方式二:命令行指定?--spring.profiles.active=dev
方式三:控制臺指定(將項目打包后在控制臺運行時調(diào)用指令)
java -jar jar包名 --spring.profiles.active=dev
?方式四:虛擬機參數(shù)指定
-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-03-17 Android跳轉三方應用實例代碼_Android
- 2021-12-06 C語言使用rand函數(shù)生成隨機數(shù)_C 語言
- 2023-02-09 使用adb?or?fastboot命令進入高通的9008(edl)模式的兩種方法_Android
- 2022-02-17 藍屏終止代碼:WHEA_UNCORRECTABLE_ERROR
- 2024-02-01 springboot @spring.active@啟動報錯
- 2023-12-19 SQLSyntaxErrorException異常產(chǎn)生原因及解決方案
- 2022-07-16 Spring MVC @Autowired和@Service注解
- 2022-10-25 python繪圖之坐標軸的超詳細講解_python
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支