-
Notifications
You must be signed in to change notification settings - Fork 28
Motivations
Dependency injection frameworks allow you to inject properties in Java objects and they do it well.
But in order to benefit from this feature, your code should run inside a DI container, or at least, the object in which you are trying to inject properties must be managed by a DI container.
What if your code does not run inside a DI container? This is where Easy Props comes to play, to allow you to benefit from dependency injection without requiring your code to run inside a DI container.
There are many great libraries about Java configuration out there and Easy Props does not compete with them. It is just providing a different approach to handle Java properties in a declarative way.
DeltaSpike and CDI extensions are out of scope since they are used in managed environments. Easy Props's goal is to be used in non-managed environments.
Clearly, Apache Commons Configuration is one of the best configuration packages. There is no problem with apache commons, but the API makes your object look for configuration properties itself:
class Config {
private String name;
public Config() {
Configurations configs = new Configurations();
try
{
Configuration config = configs.properties(new File("config.properties"));
name = config.getString("name");
}
catch (ConfigurationException cex)
{
// Something went wrong
}
}
}
When your object is looking actively for configuration properties, its design is in contradiction with the Inversion Of Control principle with all known drawbacks of such a design.
Confucius is very similar to apache commons configuration:
public class BagelShop {
public static void main(String[] args) {
Configurable config = Configuration.getInstance();
System.out.println("Hello and welcome to our store. " + config.getStringValue("slogan"));
System.out.println("Today's selection: " + config.getStringList("bagels"));
}
}
Hence, the same "problems" with this approach apply here.
Owner is probably the most complete configuration library for Java. It has a very rich features set and is very well documented. Kudos to the author!
The approach used by Owner is to define a Java interface associated to a properties file. This is a good idea indeed, as it provides a type safe way to access properties through the mapping interface. The problem with this approach raises when you have dozens or hundreds of properties in your properties files (which is often the case in production). This will make your mapping interface huge and you should write a method for every property.
The second issue with this approach is that your object is still calling the configuration interface and not provided the configuration by the library.
Tamaya is in incubation stage at apache and looks very promising. It provides a uniform API and SPI for accessing configuration, regardless if you are running in a Java SE, Java EE or OSGI environment. Even though Tamaya has a configuration API like other libraries:
Configuration config = ConfigurationProvider.getConfiguration();
String myKey = config.get("myKey"); // may return null
int myLimit = config.getInt("all.size.limit").getAsInt(); // Never returns null
It is the only library that is comparable to Easy Props since it is able to inject properties that are declared through annotations. See injection sample.
Apache Tamaya looks promising and I hope it will become a top-level apache project because I believe this is the right way to deal with configuration in Java.
Easy Properties is created by Mahmoud Ben Hassine and the awesome contributors
-
Introduction
-
Documentation