我有以下枚举:
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 变量。
这是否可能,如果不可能,我应该使用抽象类/接口进行抽象将其分成两个类吗?
那么您可以执行以下操作:
-
创建一个新类:我的枚举属性
@ConfigurationProperties(prefix = "enumProperties")
@Getter
public class MyEnumProperties {
private Map<String, Long> enumMapping;
}
-
通过以下方式为您的 SpringBootApplication/任何 Spring Config 启用 ConfigurationProperties
@EnableConfigurationProperties(value = MyEnumProperties.class)
-
现在添加您的号码应用程序属性像这样的文件:
enumProperties.enumMapping.NAME=123
enumProperties.enumMapping.FAME=456
-
在您的应用程序代码中自动装配您的属性,如下所示:
@Autowired
private MyEnumProperties properties;
-
现在这里是one way获取 id:
properties.getEnumMapping().get(MyEnum.NAME.name()); //should return 123
您可以通过这种方式为每个枚举值获取您定义的值application.properties
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)