通过属性文件中的值声明枚举

时间:2018-07-30 02:54:04

标签: spring spring-boot enums

我有这样的属性文件(application.yml):

userID:
 limitedAttempts:
 -
  bins:
  - "123456"
  attempts: 2
  bookType: ALPHA
 -
  bins:
  - "789012"
  attempts: 2
  bookType: NHANAM

属性类:

@Component
@ConfigurationProperties(prefix = "userID")
@RefreshScope
public class UserIDProperties {
    private List<LimitedAttempt> limitedAttempts = new ArrayList<>();
    ...
    public static class LimitedAttempt{
        private List<String> bins = new ArrayList<>();
        private int attempts;
        private BookType bookType;
        ...
    }
}

如何将BookType声明为具有属性文件ALPHA,NHANAM中值的枚举

如果我通过添加新元素来更新属性文件,例如:

userID:
 limitedAttempts:
 -
  bins:
  - "123456"
  attempts: 2
  bookType: ALPHA
 -
  bins:
  - "789012"
  attempts: 2
  bookType: NHANAM
 -
  bins:
  - "456789"
  attempts: 2
  bookType: FIRSTNEW

重建项目,BookType将为:ALPHA,NHANAM,FIRSTNEW。 我可以这样做吗?

*注意:我可以将BookType声明为java

public enum BookType{
    ALPHA,
    NHANAM;
}

但是它不是动态的,因为我使用领事,所以我不想在Java代码中更新。 感谢您的帮助。

0 个答案:

没有答案