Spring Boot 配置文件注入,在yml中可以自动提示

 

 

 

 

添加依赖

在pom中添加依赖

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

添加@ConfigurationProperties

在对应的类上添加 @ConfigurationProperties(prefix = "student")

package com.softtool.study.bean;

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

@Component
@ConfigurationProperties(prefix = "student")
public class Student {
    private  int id;
    private  String name;
    private  Integer age;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

从新运行一下程序

然后打开yml,输入student,就会有提示了

posted on 2020-09-04 17:42  过来啊小莲  阅读(1767)  评论(0编辑  收藏  举报

导航