SpringBoot 如何让yml,properties配置文件有提示

咱们在引用spring官方start库或者第三方start库时,在写配置文件时idea老是能精准的提示,而且鼠标能够点过去看具体属性或者类,而本身写的配置文件idea只会有“Cannot resolve configuration property ...”这样的提示。html

咱们如今也去配置咱们本身的配置文件让idea知道这些配置文件是干什么的。java

1、须要的注解

  • @ConfigurationProperties 配置属性文件,须要指定前缀 prefix
  • @EnableConfigurationProperties 启用配置,须要指定启用的配置类
  • @NestedConfigurationProperty 当一个类中引用了外部类,须要在该属性上加该注解

2、POM依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>
复制代码

3、配置类

@Data
public class School {
    private Integer no;
    private String name;
    private String address;
}
复制代码
@Data
@ConfigurationProperties(prefix = "jiuxian")
public class JiuxianProperties {
    private String name;
    private String nameCn;
    private String nameEn;
    private String[] hobbies;
    private SexEnum sexEnum;
    private boolean single;
    private School school;
    private City city;

    enum SexEnum {
        MAN, WOMAN
    }

    @Data
    static class City {
        private String no;
        private String name;
    }
}
复制代码

这个时候@ConfigurationProperties(prefix = "jiuxian") 注解会报错not registered via @EnableConfigurationProperties or marked as Spring component。这须要加上@EnableConfigurationProperties就能够了git

@EnableConfigurationProperties({ JiuxianProperties.class })
@SpringBootApplication
public class SpringbootYmlApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootYmlApplication.class, args);
    }
}
复制代码

4、看效果

而后输入 mvn clean packagegithub

这个时候properties和yml文件已经能够提示了,不过有一些会有问题,好比jiuxian.school,这是一个对象,idea并不能解析,而由于City对象是个内部类是能够解析的。spring

如图:json

如何处理呢? 只须要加上 @NestedConfigurationProperty注解便可springboot

@NestedConfigurationProperty
private School school;
复制代码

而后输入命令 mvn clean packageide

警告消失(会有一点延迟)spring-boot

5、为何

为何必定要打包编译后才能够呢?idea

看一下打包事后的结构,如图:

idea之因此会代码提示就是由于这个spring-configuration-metadata.json json文件。这个文件的生成依据的就是咱们上面的配置。

6、进阶配置

若是想在配置文件中可以提示该字段描述,以及该字段可选参数,是否弃用等信息时,须要加额外的配置

1. 在resources目录下新建META-INF文件夹,加一个additional-spring-configuration-metadata.json 配置文件

(1)该配置文件的节点信息

  • groups 有如下属性:
    • name group的全名,该属性必须
    • type group数据类型的类名。例如,若是group是基于一个被@ConfigurationProperties注解的类,该属性将包含该类的全限定名。若是基于一个@Bean方法,它将是该方法的返回类型。若是该类型未知,则该属性将被忽略
    • description 一个简短的group描述,用于展现给用户,要.点结尾。若是没有可用描述,该属性将被忽略
    • sourceType 来源类名。例如,若是组基于一个被@ConfigurationProperties注解的@Bean方法,该属性将包含@Configuration类的全限定名,该类包含此方法。若是来源类型未知,则该属性将被忽略
    • sourceMethod 该组的方法的全名(包含括号及参数类型)。例如,被@ConfigurationProperties注解的@Bean方法名。若是源方法未知,该属性将被忽略

"groups"是高级别的节点,它们自己不指定一个值,但为properties提供一个有上下文关联的分组。例如,server.port和server.servlet-path属性是server组的一部分。

注:不须要每一个"property"都有一个"group",一些属性能够以本身的形式存在。

  • properties

    • name 属性全名,格式为小写虚线分割的形式(jiuxian.name-en).必需要有的
    • type 属性数据类型,java.lang.Boolean。类型未知可忽略
    • description 该属性的描述
    • sourceType 来源类型,例如,若是property来自一个被@ConfigurationProperties注解的类,该属性将包括该类的全限定名。若是来源类型未知则该属性会被忽略
    • defaultValue 定义输入时的默认值,只是提示,并非真正的默认值,可忽略
    • deprecated 是否废弃 boolean 值
      • level 级别 error,warning
      • reason 废弃缘由
      • replacement 替代属性,为properties 全名
  • hints 能够给属性提供可选的值,以级描述

    • name 属性全名,不能为空
    • values 可选的值

详细看示例

{
  "groups": [
    {
      "name": "jiuxian",
      "sourceType": "com.jiuxian.config.JiuxianProperties",
      "type": "com.jiuxian.config.JiuxianProperties"
    },
    {
      "name": "jiuxian.school",
      "sourceType": "com.jiuxian.config.School",
      "type": "com.jiuxian.config.School",
      "sourceMethod": "getSchool()"
    }
  ],
  "properties": [
    {
      "name": "jiuxian.name",
      "sourceType": "com.jiuxian.config.JiuxianProperties",
      "type": "java.lang.String",
      "deprecation": {
        "level": "error",
        "reason": "replacement nameCn.",
        "replacement": "jiuxian.name-cn"
      }
    },
    {
      "name": "jiuxian.name-cn",
      "sourceType": "com.jiuxian.config.JiuxianProperties",
      "type": "java.lang.String",
      "defaultValue": "jiuxian"
    }
  ],
  "hints": [
    {
      "name": "jiuxian.sex-enum",
      "values": [
        {
          "value": "man",
          "description": "man."
        },
        {
          "value": "woman",
          "description": "woman."
        }
      ]
    },
    {
      "name": "jiuxian.single",
      "values": [
        {
          "value": true,
          "description": "yes."
        },
        {
          "value": false,
          "description": "no."
        }
      ]
    }
  ]
}
复制代码

idea 提示:

(2) 如何废弃某个字段,能够用代码来注解

@Deprecated
private String name;

@Deprecated
public String getName() {
    return getNameCn();
}

@DeprecatedConfigurationProperty(replacement = "jiuxian.name-cn", reason = "replacement nameCn")
public void setName(String name) {
    setNameCn(name);
}
复制代码

7、更多详细介绍请参阅官网

Spring官网介绍

8、本文示例代码

GitHub 源码

【注】以上代码基于Springboot 2.0

相关文章
相关标签/搜索