springboot配置类智能提示
实践环境
- windows10
- gradle-4.8.1
- IDEA2017.1.2
- springboot:2.0.6.RELEASE
实现过程
- 我们在使用springboot的相关依赖时经常可以在配置文件(application.yml)中得到智能提示
- 下面我们自己也来研究一番
- 一般我们都会写一个配置类,如下
@ConfigurationProperties("example.service")
public class ExampleProperties {
private String prefix="111";
private String suffix="222";
//省略getter,setter
}
example:
service:
prefix: 111
suffix: 222
- 可是这样IDEA没有任何提示,按住ctrl也无法跳转到类定义的地方
- 首先,按照官方文档的做法https://docs.spring.io/spring-boot/docs/2.0.6.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor,我把部分说明原封不动拷过来了
B.3 Generating Your Own Metadata by Using the Annotation Processor
You can easily generate your own configuration metadata file from items annotated with @ConfigurationProperties by using the spring-boot-configuration-processor jar. The jar includes a Java annotation processor which is invoked as your project is compiled. To use the processor, include a dependency on spring-boot-configuration-processor.
With Maven the dependency should be declared as optional, as shown in the following example:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
With Gradle 4.5 and earlier, the dependency should be declared in the compileOnly configuration, as shown in the following example:
dependencies {
compileOnly "org.springframework.boot:spring-boot-configuration-processor"
}
With Gradle 4.6 and later, the dependency should be declared in the annotationProcessor configuration, as shown in the following example:
dependencies {
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
If you are using an additional-spring-configuration-metadata.json file, the compileJava task should be configured to depend on the processResources task, as shown in the following example:
compileJava.dependsOn(processResources)
This dependency ensures that the additional metadata is available when the annotation processor runs during compilation.
The processor picks up both classes and methods that are annotated with @ConfigurationProperties. The Javadoc for field values within configuration classes is used to populate the description attribute.
- 由于我使用的是gradle4.8,因此按照官方的说法使用annotationProcessor,然后在resources/META-INF目录下新建了additional-spring-configuration-metadata.json
{
"properties": [
{
"sourceType": "com.example.examplespringbootstarter.bean.config.ExampleProperties",
"name": "example.service.prefix",
"type": "java.lang.String",
"description": "Description for example.service.prefix."
},
{
"sourceType": "com.example.examplespringbootstarter.bean.config.ExampleProperties",
"name": "example.service.enable",
"type": "java.lang.String",
"description": "Description for example.service.enable."
},
{
"sourceType": "com.example.examplespringbootstarter.bean.config.ExampleProperties",
"name": "example.service.suffix",
"type": "java.lang.String",
"description": "Description for example.service.suffix."
}
] }
- 注意不能使用中文,否则会有乱码,这个问题还没找到解决办法,如有人知道,请不吝赐教。
- 此时打开此json文件就会看到IDEA上这个红色的错误提示

- 出现这个错误的话那么配置文件是无法提示的,这个错误也暂时找不到解决办法。
- 于是将之前的annotationProcessor 改为compileOnly后,这个红色的错误消失,发现智能提示也有了。突然一脸懵逼,官网你写的到底正不正确啊,是不是我打开的方式不对啊?(总之,这里经过实践就不要照着官网的了)
- 如果发现还是无效的话那么请在项目目录下执行命令:gradlew assemble, 或者如图双击assemble
