I want to use an Enum in Java for storing configuration values for different environments. Each Enum will have the same fields, but different values. Something like:
public enum DevelopmentConfig
{
URL("..."),
defaultURL(".....");
}
public enum ProductionConfig
{
URL("..."),
defaultURL(".....");
}
This is for a web application, so I can’t simply use Preferences or any other solution.
My question is, is there a way to create an interface to define the fields of the configuration? Or should I be using a normal class instead of enum for storing these values?
Edit: To use this, I simply want to do this from my other classes:
String url = Config.URL
Or
String url = Config.getURL();
Without knowing if that refers to Config.Development or Config.Production (I want that to be determined in the Config enum’s constructor itself and have it choose the right set of fields)
Each member of the enum is an instance of the enum class. That means that you can define methods, variables and implement interfaces:
If you’re looking to use enums to look-up values, I would recommend using them as a key in a Map instead of implementing different types per need.
Edit
You can accomplish this by reading in a .property file from a location in the environment your application is running in (dev / prod / etc), then keying into the property file with the enum:
PropertyManager’s API would look like the following: