很多開發(fā)者在使用Spring Boot進行開發(fā)的時候可能會使用到配置旷赖,先在application.properties中配置顺又,然后在具體使用的類中使用@Value注解獲取值。當配置太多的時候等孵,就會用到@ConfigurationProperties來直接加載多個配置項稚照。
但是在使用的時候可能會存在idea不提示配置,配置是正確的俯萌,但是還是顯示黃色的警告波浪線
在使用到@ConfigurationProperties的類中也會在頂部出現(xiàn)Spring Boot Configuration Annotation Processor not configured果录,然后選擇Open Documentation打開鏈接的時候,地址是錯誤的,正確的地址:https://docs.spring.io/spring-boot/docs/2.2.6.RELEASE/reference/html/appendix-configuration-metadata.html#configuration-metadata
出現(xiàn)這種問題是因為缺少引入spring-boot-configuration-processor包的依賴
Maven
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Gradle 4.5及其之前的版本
dependencies {
compileOnly "org.springframework.boot:spring-boot-configuration-processor"
}
Gradle 4.6及其之后的版本
dependencies {
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
在配置依賴并且刷新依賴之后在@ConfigurationProperties類會出現(xiàn):Re-run Spring Boot Configuration Annotation Processor to update generated metadata咐熙,這時候只需要Hide隱藏就可以了
在idea中配置項目生成注解
如果你是用的是gradle弱恒,執(zhí)行gradle booJar之后,會在classes下生成spring-configuration-metadata.json文件棋恼,如果對這個文件不了解的返弹,可以去搜索一下Spring Boot Starter開發(fā)之類的教程進行了解,這個文件里面存儲的是配置的描述信息
最后你會發(fā)現(xiàn)配置已經沒有警告蘸泻,并且已經能提示了