Spring Boot 常見錯誤及解決方法

2020-12-15 小鋒學長

Spring Boot 作為 Java 生態中最流行的開發框架,意味著被數以萬計的開發者所使用。下面根據我們自身遇到的問題,加上用戶提供的一些反饋,來大致梳理下 Spring Boot 的常見錯誤及解決方法。

找不到配置?配置不對?配置被覆蓋?

Spring Boot 配置加載過程解析:

1、Spring Boot 配置的加載有著約定俗成的步驟: 從 resources 目錄下加載 application.properties/application.yml ; 再根據裡面的 spring.profiles.active 來加載不同 profile 的配置文件 application-dev.properties/application-dev.yml (比如加載 profile 為 dev 的配置文件)。

2、Spring Boot 所有的配置來源會被構造成 PropertySource,比如 -D 參數, – 參數, 系統參數, 配置文件配置等等。這些 PropertySource 最終會被添加到 List 中,獲取配置的時候會遍歷這個 List ,直到第一次獲取對應 key 的配置,所以會存在優先級的問題。具體配置的優先級參考:

https://stackoverflow.com/a/45822571

配置覆蓋案例:

Nacos 服務註冊的 IP 可以通過 spring.cloud.nacos.discovery.ip 設置,當我們打成 JAR 包之後,如需修改註冊 IP,可以通過 -Dspring.cloud.nacos.discovery.ip=xxx(-D 參數配置的優先級比配置文件要高)。

配置問題排查:

進入 http://host :port/actuator/env 這個 endpoint 查看具體的配置項屬於哪個 PropertySource。

Jar 包啟動不了

執行 Spring Boot 構建的 jar 包後,返回 「my.jar 中沒有主清單屬性」 錯誤。

錯誤分析: Spring Boot 的正常 jar 包運行方是通過 spring-boot-loader 這個模塊裡的 JarLauncher 完成的,該類內部提供了一套運行的規範。

解決方案: 在 pom 裡加上 spring-boot-maven-plugin 的 maven 插件配置 (該插件會在 jar 裡加入 spring-boot-loader 的代碼,並在 MANIFEST.MF 中的 Main-Class 裡寫入 JarLauncher):

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

自動化配置類沒有被加載

條件註解是 Spring Boot 的核心特性之一,第三方的 starter 或我們自定義的 starter 內部都會加載一些 AutoConfiguration,有時候會存在一些 AutoConfiguration 沒有被加載的情況。

導致出現 NoSuchBeanDefinitionException, UnsatisfiedDependencyException 等異常排查步驟 (三種方式):

1、把 spring 的日誌級別調到 debug 級別: logging.level.org.springframework: debug。

2、從 ApplicationContext 中獲取 ConditionEvaluationReport,得到內部的 ConditionEvaluationReport.ConditionAndOutcomes 類中的輸出信息。

3、進入 http://host :port/actuator/conditions 這個 endpoint 查看條件註解的 match 情況。

這是日誌列印的不滿足條件的 AutoConfiguratoin:

Unconditional classes:

----------------------

org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

org.springframework.cloud.client.ReactiveCommonsClientAutoConfiguration

org.springframework.boot.actuate.autoconfigure.info.InfoContributorAutoConfiguration

org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration

org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration

org.springframework.cloud.client.CommonsClientAutoConfiguration

org.springframework.cloud.commons.httpclient.HttpClientConfiguration

org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration

org.springframework.cloud.loadbalancer.config.BlockingLoadBalancerClientAutoConfiguration

定義的 Component 沒有被掃描到

@SpringBootApplication 註解內部也會使用 @ComponentScan 註解用於掃描 Component 。默認情況下會掃描 @SpringBootApplication 註解修飾的入口類的包以及它下面的子包中所有的 Component 。

@ComponentScan:

https://github.com/StabilityMan/StabilityGuide/blob/master/ComponentSc?an

這是推薦的包結構中項目的結構:

exclude 包下的類不會被掃描到,card 包下的類會被掃描到。

Actuator Endpoint 訪問不了

訪問 Actuator,出現 404 錯誤。

解決方案:

1、Spring Boot 2.x 版本對 Actuator 做了大量的修改,其中訪問的路徑從

http://host :port/endpointid

變成了

http://host :port/actuator/endpointid 。

確保訪問的路徑正確。

2、Endpoint 有 Security 要求,

在配置裡加上 management.endpoints.web.exposure.include=* 即可。

作者介紹:

方劍,花名洛夜,GitHub ID @fangjian0423,開源愛好者,阿里巴巴高級開發工程師,阿里雲產品 EDAS 開發,Spring Cloud Alibaba 開源項目負責人。

相關焦點

  • spring boot 整合shiro 錯誤
    最近在弄spring boot 整合shiro的。這裡記錄其中一個錯誤:1:No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.
  • Spring Boot Admin 2.2.4 發布,兼容最新版本 Spring Boot
    spring boot admin 2.2.4 版本發布,本版本為 bug 修復版本 主要兼容 spring boot 2.3.x。
  • Spring Boot集成JDBCTemplate
    JdbcTemplate主要方法JdbcTemplate主要提供以下五類方法:execute方法:可以用於執行任何SQL語句,一般用於執行DDL語句;update方法及batchUpdate方法。update方法用於執行新增、修改、刪除等語句;batchUpdate方法用於執行批處理相關語句;query方法及queryForXXX方法,用於執行查詢相關語句;call方法:用於執行存儲過程、函數相關語句。
  • Spring SPI和Spring Boot SPI - 第345篇
    那麼同樣的道理,spring-boot-autoconfigure模塊也能動態加載了。org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\org.springframework.boot.autoconfigure.condition.OnBeanCondition,\org.springframework.boot.autoconfigure.condition.OnClassCondition
  • Spring Boot 2.4 手工和 SDKMAN! 安裝 Spring Boot 命令行
    可用的下載地址,請參考下面的連結:spring-boot-cli-2.4.2-SNAPSHOT-bin.zipspring-boot-cli-2.4.2-SNAPSHOT-bin.tar.gzSapshot 版本下載,snapshot 版本的意思是從最新的原始碼庫中進行編譯構建的,通常這個版本具有更多的 Bug 修復,下載地址請訪問下面的連結: snapshot 構建版本
  • 2021 最新版 Spring Boot 速記教程
    true,後面的方法才會繼續執行。<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-aop</artifactId></dependency>
  • Spring Boot 和 Spring 到底有啥區別?
    Web應用程式:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.0.6.RELEASE<
  • Spring 和 Spring Boot 之間到底有啥區別?
    ><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.0.6.RELEASE</version></dependency
  • Spring Boot Security 詳解
    它提供全面的安全性解決方案,同時在 Web 請求級和方法調用級處理身份確認和授權。工作流程從網上找了一張Spring Security 的工作流程圖,如下。 INSERT INTO role_permission (role_id, permission_id) VALUES (2, 1);INSERT INTO role_permission (role_id, permission_id) VALUES (2, 2);pom.xml<dependency> <groupId>org.springframework.boot
  • Spring Boot 單元測試
    單元測試引用:眾所周知,通過spring initialize創建的Spring Boot項目會在Maven中自動攜帶很多starter依賴:其中包含了一個名為spring-boot-starter-test的依賴,本文是圍繞這個依賴展開。
  • Spring Boot中使用Mockito進行Web測試 - 第339篇
    好了進入正題,我們添加web依賴:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId><
  • Spring 和 Spring Boot 最核心的 3 大區別,詳解!
    <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId>    <version>2.0.6.RELEASE<
  • Spring Boot 整合 Thymeleaf
    同時能夠作為靜態引擎,讓開發成員之間更方便協作開發;Spring Boot 官方推薦使用模板,而且 Spring Boot 也為 Thymeleaf 提供了完整的自動化 配置解決方案;Thymeleaf 使用教程請戳 Tutorial: Using Thymeleaf[1],配合 Spring 使用的教程請戳
  • Spring Boot 採用Sharding-JDBC 實現Mybaits的分庫分表功能
    業內其實也有很多比較成熟的解決方案,如:Cobar、Cobar-client、TDDL、Sharding-JDBC等,這次就拿當當網開源的Sharding-JDBC來實現基於Mybatis的分庫分表功能。
  • Spring Boot 2.X 實戰--SQL 資料庫(MyBatis)
    mybatis-spring-boot-starter 支持傳統的 Mapper.xml 的配置方法;支持幾乎沒有配置的註解方式。本小節主要使用註解的方式,Mapper.xml 方式也會介紹。Gradle 依賴 1dependencies { 2    implementation 'org.springframework.boot:spring-boot-starter-web' 3    implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter
  • 使用IntelliJ IDEA創建一個Maven的Spring Boot項目
    </groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent>
  • 超詳細Spring Boot面試問題集錦,死角一個不留!
    全文共2564字,預計學習時長5分鐘本文將討論Spring Boot中最常見的10個面試問題。在當今就業市場中,這些問題出現的頻率呈上升趨勢並且有一些棘手。1. @SpringBootApplication注釋在內部有什麼用處?
  • Spring Boot中Tomcat、Jetty、Undertow的使用
    Tomcat在我們使用SpringBoot開發WebApi時,會引入spring-boot-starter-web這個starter組件,其自帶了Tomcat容器,所以我們平時新建項目啟動起來,會看見Tomcat相關的一些信息。
  • Spring Boot 的單元測試和集成測試
    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope></dependency
  • Spring Boot 集成undertow作為web容器
    一、先去除tomcat的引用<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web