Spring Boot自定義配置實現IDE自動提示
阿新 • • 發佈:2019-12-31
一、背景
官方提供的spring boot starter的配置項,我們用IDE配置的時候一般都有自動提示的,如下圖所示
而我們自己自定義的配置卻沒有,對開發非常不友好容易打錯配置,那這個是怎樣實現的呢?
二、提示原理
IDE是通過讀取配置資訊的元資料而實現自動提示的,而元資料在目錄META-INF
中的spring-configuration-metadata.json
或者 additional-spring-configuration-metadata.json
三、實現自動提示
以我這個自己開發的starter中的自定義配置檔案為例,如果自己手動建立這些元資料的話工作量比較大,使用IDEA
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
3.3. 重新編譯專案
專案在重新編譯後就會自動生成spring-configuration-metadata.json
檔案
四、測試
自定義的swagger配置已經能自動提示了
參考資料 docs.spring.io/spring-boot…
推薦閱讀