【问题标题】:Configuring an enum in Spring using application.properties使用 application.properties 在 Spring 中配置枚举
【发布时间】:2018-12-17 11:57:14
【问题描述】:

我有以下枚举:

public enum MyEnum {
    NAME("Name", "Good", 100),
    FAME("Fame", "Bad", 200);

    private String lowerCase;
    private String atitude;
    private long someNumber;

    MyEnum(String lowerCase, String atitude, long someNumber) {
        this.lowerCase = lowerCase;
        this.atitude = atitude;
        this.someNumber = someNumber;
    }
}

我想使用 application.properties 文件为枚举的两个实例设置不同的 someNumber 变量。 这可能吗?如果没有,我是否应该使用抽象类/接口将其分成两个类进行抽象?

【问题讨论】:

    标签: java spring spring-boot enums


    【解决方案1】:

    您不能/不应该更改 Java 中枚举的值。尝试改用一个类:

    public class MyCustomProperty { 
        // can't change this in application.properties
        private final String lowerCase;
        // can change this in application.properties
        private String atitude;
        private long someNumber;
    
        public MyCustomProperty (String lowerCase, String atitude, long someNumber) {
            this.lowerCase = lowerCase;
            this.atitude = atitude;
            this.someNumber = someNumber;
        }
        // getter and Setters
    }
    

    比创建自定义ConfigurationProperties:

    @ConfigurationProperties(prefix="my.config")
    public class MyConfigConfigurationProperties {
        MyCustomProperty name = new MyCustomProperty("name", "good", 100);
        MyCustomProperty fame = new MyCustomProperty("fame", "good", 100);
    
        // getter and Setters
    
        // You can also embed the class MyCustomProperty here as a static class. 
        // For details/example look at the linked SpringBoot Documentation
    }
    

    现在您可以在 application.properties 文件中更改 my.config.name.someNumbermy.config.fame.someNumber 的值。如果您想禁止更改小写/态度,请将它们设为最终版本。

    在使用它之前,您必须使用 @EnableConfigurationProperties(MyConfigConfigurationProperties.class) 注释 @Configuration 类。还要添加 org.springframework.boot:spring-boot-configuration-processor 作为可选依赖项以获得更好的 IDE 支持。

    如果你想访问这些值:

    @Autowired
    MyConfigConfigurationProperties config;
    ...
    config.getName().getSumeNumber();
    

    【讨论】:

    • 非常感谢这个解决方案,这正是我所需要的。
    【解决方案2】:

    那么你可以做的是:

    1. 创建一个新类:MyEnumProperties

      @ConfigurationProperties(prefix = "enumProperties")
      @Getter
      public class MyEnumProperties {
      
          private Map<String, Long> enumMapping;
      
      }
      
    2. 通过

      为您的 SpringBootApplication/任何 Spring Config 启用 ConfigurationProperties
      @EnableConfigurationProperties(value = MyEnumProperties.class)
      
    3. 现在将您的号码添加到 application.properties 文件中,如下所示:

      enumProperties.enumMapping.NAME=123
      enumProperties.enumMapping.FAME=456
      
    4. 在您的应用程序代码中自动装配您的属性,如下所示:

      @Autowired
      private MyEnumProperties properties;
      
    5. 现在这是获取 ID 的一种方式

      properties.getEnumMapping().get(MyEnum.NAME.name()); //should return 123
      

    您可以通过这种方式为每个 Enum 值获取 application.properties 中定义的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 2016-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多