Spring配置属性提示在应用程序yml文件中定义它们时未显示

时间:2019-02-21 15:06:14

标签: java spring-boot intellij-idea

我只是在使用IntelliJ作为ID的Spring Boot中尝试使用类型安全的配置属性。我在定义applicatin.yml文件内的那些属性时遇到提示的问题。提示只要它简单的POJO不能与bean的集合一起使用,就可以很好地工作。例如:关于以下代码,当我在application.yml文件中指定admins的属性时,我只是得到rest-api.admins作为提示。它不能指导我在集合内指定内部对象的属性。 enter image description here  这是我的示例代码:

@ConfigurationProperties(
        prefix = "rest-api",
        ignoreUnknownFields = true
)
@Component
public class RestApiProperties {

    private static final List<Person> DEFAULT_ADMIN = Arrays.asList(defaultAdmin());

    @NestedConfigurationProperty
    private Person person = new Person();

    private List<Person> admins = DEFAULT_ADMIN;


    public Person getPerson() {
        return person;
    }

    public List<Person> getAdmins() {
        return admins;
    }

    public void setAdmins(List<Person> admins) {
        this.admins = admins;
    }

    private static Person defaultAdmin() {
        Person person = new Person();
        person.setName("defaultAdmin");
        person.setAge(34);
        person.setSex(MALE);
        return person;
    }

让我的pojo Person处于不同的班级:

public class Person {
    private static final String DEFAULT_NAME = "defaultUser";
    private static final int DEFAULT_AGE = 23;
    private static final Sex DEFAULT_SEX = Sex.MALE;

    private String name = DEFAULT_NAME;
    private int age = DEFAULT_AGE;
    @NestedConfigurationProperty
    private Sex sex = DEFAULT_SEX;

    public Sex getSex() {
        return sex;
    }

    public void setSex(Sex sex) {
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

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

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

性别下的枚举类

public enum Sex {
    MALE, FEMALE
}

我添加了Additional-spring-configuration-metadata.json并放置在META-INF文件夹中。

{
  "groups": [
    {
      "name": "rest-api",
      "type": "yadavsudhir405.rest.github.com.configurationProperty.RestApiProperties"
    },
    {
      "name": "rest-api.person",
      "type": "yadavsudhir405.rest.github.com.configurationProperty.Person"
    }
  ],
  "properties": [
    {
      "name": "rest-api.person.name",
      "type": "java.lang.String"
    },
    {
      "name": "rest-api.person.age",
      "type": "java.lang.Integer"
    },
    {
      "name": "rest-api.person.sex",
      "type": "yadavsudhir405.rest.github.com.configurationProperty.Sex"
    },
    {
      "name": "rest-api.admins",
      "type": "java.util.List<yadavsudhir405.rest.github.com.configurationProperty.Person>"
    }

  ],
  "hints": []
}

0 个答案:

没有答案