1. 程式人生 > 程式設計 >Spring Boot自定義配置實現IDE自動提示

Spring Boot自定義配置實現IDE自動提示

file

一、背景

官方提供的spring boot starter的配置項,我們用IDE配置的時候一般都有自動提示的,如下圖所示

file
而我們自己自定義的配置卻沒有,對開發非常不友好容易打錯配置,那這個是怎樣實現的呢?

 

二、提示原理

IDE是通過讀取配置資訊的元資料而實現自動提示的,而元資料在目錄META-INF中的spring-configuration-metadata.json 或者 additional-spring-configuration-metadata.json

 

三、實現自動提示

以我這個自己開發的starter中的自定義配置檔案為例,如果自己手動建立這些元資料的話工作量比較大,使用IDEA

的話有自動生成功能

file

 

3.1. 引入依賴spring-boot-configuration-processor

zlt-swagger2-spring-boot-starter工程中新增以下jar包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency
>
複製程式碼

 

3.2. 修改IDEA配置

搜尋Annotation Processor並設定Enable annotation processing

file

 

3.3. 重新編譯專案

專案在重新編譯後就會自動生成spring-configuration-metadata.json檔案

file

 

四、測試

自定義的swagger配置已經能自動提示了

file

  參考資料 docs.spring.io/spring-boot…

  推薦閱讀