springboot-yaml配置文件提示

luckyFang 2021年11月17日 24次浏览

引言

在前面的学习中我们学会了使用springboot yaml配置文件。

但是我们发现我们自己配置的和默认的似乎有些不同,就是默认的配置选项他会有提示,通过阅读springboot 官方文档后找到了解决方案。

新建实体类

Person.java

package com.example.bootyaml.entity;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

@ConfigurationProperties(prefix = "person")
@Component
@Data
public class Person {
    private String name;
    private Integer age;
    private List<String> hobby;
    private HashMap<String, Set<String>> techStack;
    private Pet pet;
}

Pet.java

package com.example.bootyaml.entity;

import lombok.Data;

@Data
public class Pet {
    private String name;
    private Double weight;
}

配置yaml提示

我们先看看官方文档怎么说的

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.
This dependency ensures that the additional metadata is available when the annotation processor
runs during compilation.

也就是我们需要spring-boot-configuration-processor 这个依赖他可以确保你在外部配置文件中实现属性提示

添加maven依赖

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

然后我们再试试

image.png

大功告成