springboot辅助配置工具:spring-boot-configuration-processor

原文链接 https://blog.csdn.net/m0_37529488/article/details/122639916

在SpringBoot的项目中,往往需要一些配置,使用idea添加配置有些都会给提示,这个是怎么实现的呢。如下图
springboot辅助配置工具:spring-boot-configuration-processor_第1张图片
自定义配置怎么出现提示呢?

使用spring-boot-configuration-processor实现,其作用是生产配置元数据。

  1. 添加下列依赖
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-configuration-processorartifactId>
            <optional>trueoptional>
        dependency>
  1. 在resources添加/META-INF/spring-configuration-metadata.json

json文件如下格式:

{
  "groups": [],
  "hints": [],
  "properties": [
    {
      "name": "muser.name",
      "type": "java.lang.String",
      "description": "username"
    },
    {
      "name": "muser.password",
      "type": "java.lang.String",
      "description": "password"
    },
    {
      "name": "muser.host",
      "type": "java.lang.String",
      "defaultValue": "localhost",
      "description": "hostname"
    },
    {
      "name": "muser.port",
      "type": "java.lang.Integer",
      "defaultValue": "8080",
      "description": "port"
    }
  ]
}
  1. 对应的Properties:
@ConfigurationProperties(prefix = "muser")
@Component
public class UserProperties {
    private String name;
    private String password;
    private String ip;
    private int port;
 
   //getter setter...
}
  1. 出现的效果如下:

springboot辅助配置工具:spring-boot-configuration-processor_第2张图片

注:配置中的默认值不代表Properties中已经设置,spring-boot-configuration-processor仅仅只是辅助作用

你可能感兴趣的:(springboot,spring,boot)