springboot配置类智能提示

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

在这里插入图片描述

参考

https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/html/configuration-metadata.html#configuration-metadata-annotation-processor

https://blog.csdn.net/L_Sail/article/details/70342023

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值