SpringBoot如何实现自定义配置文件提示

SpringBoot如何实现自定义配置文件提示

文章目录

  • SpringBoot如何实现自定义配置文件提示
    • 1、编写一个配置类
    • 2、引入自动提示插件
      • 2.1、引入插件
      • 2.2、重新编译
    • 3、配置验证

我们在使用SpringBoot开发项目时,常常需要编写一些属性配置类,用来完成自定义或特定的属性配置。在我们配置application.properties时,IDEA会自动提示框架的相关配置,但是我们自己编写的特定的属性配置却不会自动提示。本文介绍了相关的插件,可以实现自定义配置文件的属性提示

1、编写一个配置类

我们编写一个配置类

  • Person
/**
 * @author zhang_wei
 * @version 1.0.0
 * @Classname Person
 * @Date 2021/3/20 0:07
 * @Created by zhang_wei
 * @since 1.0.0
 */
@Component
@ConfigurationProperties(prefix = "person")
public class Person {

    String name;

    int age;

    boolean boss;

    Date birth;

    BigDecimal sal;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean getBoss() {
        return boss;
    }

    public void setBoss(boolean boss) {
        this.boss = boss;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public BigDecimal getSal() {
        return sal;
    }

    public void setSal(BigDecimal sal) {
        this.sal = sal;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", boss=" + boss +
                ", birth=" + birth +
                ", sal=" + sal +
                '}';
    }
}

2、引入自动提示插件

2.1、引入插件

SpringBoot自带一个插件,我们只需要在pom.xml文件中引入即可;

pom.xml中引入如下的插件:


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-configuration-processorartifactId>
dependency>

2.2、重新编译

执行一下maven的重新编译命令:mvn clean compile

或者如下,直接使用idea自带的maven工具,重新编译一下代码即可生效

SpringBoot如何实现自定义配置文件提示_第1张图片

3、配置验证

这里我们配置我们的配置文件application.properties,idea会自动提示我们相关的属性配置;

如下:

SpringBoot如何实现自定义配置文件提示_第2张图片

你可能感兴趣的:(SpringBoot,Spring,后台,spring,spring,boot,java,服务器)