SpringBoot啟動失敗Application?run?failed的問題分析及解決辦法
這個異常的問題源比較多,得具體問題具體分析,得看下面那些報錯什么意思,不能直接去網(wǎng)上找解決辦法
前言
Navicat 能看本地數(shù)據(jù)庫和服務(wù)器數(shù)據(jù)庫,具體是哪里的數(shù)據(jù)庫要看清楚,最好cmd命令show databases看一下
項目場景一:Application run failed
提示:SpringBoot服務(wù)啟動的時候報錯:
這個問題遇到好多次了,一直沒能記錄下來,今天又碰到了

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. ERROR 1572 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
?? 錯誤總覽
下面是錯誤報告我用回車隔開方便查看
Error creating bean with name 'userAction': Unsatisfied dependency expressed through field 'userService'; nested exception is ... Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': nested exception is ... Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [...] nested exception is ... Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' ... nested exception is ... Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]... nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [.../mapper/UserMapper.xml]'... nested exception is org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 36; 在 XML 聲明中的編碼偽屬性前面必須有空格。
?? 錯誤根本原因
UserMapper.xml 文件的第一行 XML 聲明格式不正確。
具體報錯是:
在 XML 聲明中的編碼偽屬性前面必須有空格。
這個錯誤通常出現(xiàn)在 XML 文件的最開始一行,也就是:
<?xml version="1.0" encoding="UTF-8"?><!-- 正確寫法 -->
但你的文件可能寫成了:
<?xml version="1.0"encoding="UTF-8"?> <!-- ? 錯誤:沒有空格 -->
或者:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
其中某個地方少了空格、引號沒閉合、語法不對等。
? 解決方案
? 第一步:檢查UserMapper.xml的第一行
打開文件:
E:\桌面\springbootdemo\target\classes\mapper\UserMapper.xml
(注意:這個路徑是你編譯后的路徑,實際修改應(yīng)該去源碼目錄下的 XML 文件)
找到類似這一行:
<?xml version="1.0"encoding="UTF-8"?>
改成:
<?xml version="1.0" encoding="UTF-8"?>
確保:
version和encoding屬性之間有空格encoding="UTF-8"寫法正確- 沒有多余的字符或拼寫錯誤
? 第二步:驗證 XML 是否語法正確
你可以將整個 XML 文件粘貼到在線 XML 驗證工具中,例如:
- https://www.xmlvalidation.com/
它會告訴你哪一行哪個位置出錯了。
? 第三步:重新構(gòu)建項目
修改完 XML 后,在命令行執(zhí)行:
mvn clean install # 或者 gradle clean build
然后重啟 Spring Boot 應(yīng)用。
?? 示例修復前 vs 修復后
? 錯誤示例:
<?xml version="1.0"encoding="UTF-8"?>
<mapper namespace="cn.edu.neu.springbootdemo.mapper.UserMapper">
...
</mapper>? 正確示例:
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="cn.edu.neu.springbootdemo.mapper.UserMapper">
...
</mapper>?? 小貼士
| 問題 | 建議 |
|---|---|
| XML 報錯定位難 | 使用 IDE 打開 XML 文件,一般會有語法高亮提示 |
| 編碼格式混亂 | 確保文件保存為 UTF-8 格式 |
| 中文亂碼 | 在 XML 頭部聲明 encoding=“UTF-8” 并保持文件編碼一致 |
| Mapper 路徑未掃描到 | 檢查是否配置了 MyBatis 的 mapper-locations |
? 總結(jié)
| 步驟 | 內(nèi)容 |
|---|---|
| 1?? | 定位到 UserMapper.xml 文件 |
| 2?? | 修改 XML 聲明頭,補上空格,如:<?xml version="1.0" encoding="UTF-8"?> |
| 3?? | 清理并重新構(gòu)建項目 |
| 4?? | 重啟應(yīng)用 |
場景分析二:Application run failed

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-10-14 13:15:21.461 ERROR 12548 --- [
main] o.s.boot.SpringApplication
: Application run failed原因分析
往下看原因:Caused by:
下面是錯誤報告我用回車隔開方便檢查
Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/spring/boot/starter/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is com.baomidou.mybatisplus.exceptions.MybatisPlusException: Error: GlobalConfigUtils setMetaData Fail ! Cause:java.sql.SQLSyntaxErrorException: Unknown database 'jiakao'
重點在最后一句,他說我這個錯誤是:GlobalConfigUtils setMetaData失??!
原因:java.sql SQLSyntaxErrorException:未知數(shù)據(jù)庫“jiakao”
解決辦法
上面的錯誤說數(shù)據(jù)庫名字不對
打開數(shù)據(jù)庫可視化工具一看,是數(shù)據(jù)庫名字打錯了
果然數(shù)據(jù)庫名字一改后,立馬跑通
總結(jié):
重要的不是問題解決了,重要的是解決問題的過程
還有error才會影響程序的運行,warning警告不用管
根據(jù)錯誤的提示去想,為什么找不到這個bean/class?通過這個錯誤一步步去跟蹤,找到根本的原因,這是積累知識的過程
等以后這種問題發(fā)現(xiàn)地慢慢的多起來,慢慢地積累起來,積累的經(jīng)驗越多越好
英語很重要,不僅僅是處理問題的時候、出錯的時候、分析問題的時候都是英文
到此這篇關(guān)于SpringBoot啟動失敗Application run failed的解決辦法的文章就介紹到這了,更多相關(guān)SpringBoot啟動失敗Application run failed內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Redis啟動失敗的常見錯誤以及解決方法
- SpringBoot項目啟動失敗的常見錯誤總結(jié)
- Springboot項目啟動失敗提示找不到dao類的解決
- SpringBoot應(yīng)用啟動失?。憾丝谡加脤е耇omcat啟動失敗的問題分析與解決方法
- SpringBoot啟動失敗的原因及其解決方法
- 解決springboot項目啟動失敗Could not initialize class com.fasterxml.jackson.databind.ObjectMapper問題
- SpringBoot啟動失敗的解決方法:A component required a bean of type ‘xxxxxxx‘ that could not be found.
- SpringBoot之ApplicationRunner解析(spring容器啟動完成執(zhí)行的類)
相關(guān)文章
Spring boot 應(yīng)用實現(xiàn)動態(tài)刷新配置詳解
這篇文章主要介紹了spring boot 配置動態(tài)刷新實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2021-09-09
構(gòu)建SpringBoot+MyBatis+Freemarker的項目詳解
在本篇內(nèi)容里小編給大家整理的是關(guān)于構(gòu)建SpringBoot+MyBatis+Freemarker的項目的具體步驟以及實例代碼,需要的朋友們參考下。2019-06-06
SpringBoot使用Spring Security實現(xiàn)登錄注銷功能
這篇文章主要介紹了SpringBoot使用Spring Security實現(xiàn)登錄注銷功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-09-09
Java實現(xiàn)發(fā)送短信驗證碼+redis限制發(fā)送的次數(shù)功能
這篇文章主要介紹了Java實現(xiàn)發(fā)送短信驗證碼+redis限制發(fā)送的次數(shù),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04

