Type-Safe Property Access in Java: The Owner Library Revolution

The Owner Library offers a modern approach to handling Java properties by providing an interface-based, type-safe way to access configuration values. Instead of traditional string-based property access, developers can define strongly-typed interfaces with annotation-based configuration, enabling compile-time checking, default values, and enhanced maintainability. This approach eliminates common property-related errors and makes refactoring easier while maintaining the simplicity of property file usage.

1 Minutes reading time

Behold the masterpiece that AI hallucinated while reading this post:

"Little Config's Big Adventure: How Properties Found Their Safe Home"

(after I fed it way too many marketing blogs and memes)

Created using DALL-E 3

AI-Generated: Little Config's Big Adventure: How Properties Found Their Safe Home

Recently i stumbled across the Owner Library(owner.aeonbits.org ). Using this small piece of technology, usage of Java property files can be reinvented! Now we can access properties using a refactoring safe interface the following way:

public interface ServerConfig extends Config {
    @Key("server.http.port")
    int port();


    @Key("server.host.name")
    String hostname();


    @Key("server.max.threads");
    @DefaultValue("42")
    int maxThreads();
}

Properties props = new Properties(); // Load properties from a known source


ServerConfig cfg = ConfigFactory
    .create(ServerConfig.class, props);


System.out.println(cfg.hostname()); // And simply use them

This is a really nice way to deal with property files:-)

Git revision: 2e692ad