Thursday, October 10, 2013

Better Java properties handling

Luigi R. Viggiano provides OWNER API, a clever, better way to handle Java properties.

Essentially he maps properties in a general sense onto Java interfaces, using annotations to guide sourcing and processing. This is going directly into my projects.

I wrote a similar library based on interfaces to access properties, but using inheritance, implementation and generics with a set of helper base classes. His approach is superior.

@Sources({ "file:~/.myapp.config", 
           "classpath:foo/bar/" })
public interface ServerConfig extends Config {
    int port();

    String hostname();

    int maxThreads();

Interfaces to access properties is right. Among benefits you get strong typing of configuration and your IDE provides code completion.

Post a Comment