简体   繁体   English

如何使java枚举元素可配置

[英]how to make java enum elements configurable

hello guys i'm not sure if the title is descriptive enough.what i mean is creating an enum like so 你好,我不确定标题是否足够描述。我的意思是创建一个这样的枚举

public enum Test{
  ONE, TWO ,THREE
}

this looks like hard coded.if for some reason i need to add the FOUR some certain business rules evolution reasons.should i code it and deploy it again? 这看起来像硬编码。如果由于某种原因我需要添加四个某些业务规则的演变原因。我应该编码并再次部署它吗? isn't a way to let it pick the elements from a file , spring config for example or property file? 是不是让它从文件,例如spring配置或属性文件中选择元素的方法? THanks for reading. 谢谢阅读。

If the enum value doesn't explicitly exist in code, how could you ever use it? 如果代码中没有明确存在枚举值,您怎么能使用它? Test.Four would not compile. Test.Four不会编译。 Any code which could somehow reference Test.Four would be invalid and would crash, until the point in time when the file is read and the new values are added. 任何可能以某种方式引用Test.Four代码都将无效并且会崩溃,直到读取文件并添加新值的时间点。

You can, of course, use arrays or collections of values and manipulate those at runtime - load them from a file or from the database or whatever - but not enums. 当然,您可以使用数组或值集合并在运行时操作它们 - 从文件或数据库或其他任何内容加载它们 - 但不是枚举。

I asked a similar question here . 在这里问了一个类似的问题。 The content may be of interest. 内容可能是有意义的。

The concensus seemed to be that Java's enum type is static by design. 共识似乎是Java的枚举类型在设计上是静态的。 If you need something that can be altered at runtime, you should ideally use a different data structure. 如果您需要可在运行时更改的内容,则理想情况下应使用不同的数据结构。

The less preferred ideas were along the lines of extending Enum, etc. 不太喜欢的想法是扩展Enum等。

您可以存储在数据库表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM