亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

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

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/ComponentScan

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

Spring Boot 常見錯誤及解決方法

 

  • 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=* 即可。

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

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/ComponentScan

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

  • 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=* 即可。

查看更多:https://yqh.aliyun.com/detail/6459?utm_content=g_1000105546

上云就看云棲號:更多云資訊,上云案例,最佳實踐,產品入門,訪問:https://yqh.aliyun.com/

分享到:
標簽:Spring Boot
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定