Configuration Injection with Tamaya

In this short blog, I would like to show how some of the configuration injection features of Apache Tamaya. The cool thing is, you dont’t need to use CDI, the injection mechanisms of Tamaya also work with Spring or even standalone…

Configuring non annotated classes/interfaces

What is an absolute minimum approach for configuring something: let your configuration framework guess what should be done! So let’s start with a simple class:

package configsample;

public final class MyConfig{
    private int port;
    private String name;
    private File location;

    public int port(){return port;}
    public String name(){return name:}
    public File file(){return file;}
}

Now we would like to populate this class with configuration. To do so we will pass it to Tamaya’s configuration injector, which is part of the tamaya-inject-se module:

MyConfig config = new MyConfig();
ConfigurationInjection.getConfigurationInjector().configure(config);

This will populate the instance with configured data that is currently provided by the global default configuration, which can be obtained from the ConfigurationProvider:

Configuration config = ConfigurationProvider.getConfiguration();

To let this code do something useful, we must add the corresponding configuration properties. By default Tamaya is reading properties from the classpath under META-INF/javaconfiguration.properties, so lets add the following properties:

configsample.MyConfig.port=8088
configsample.MyConfig.name=http
configsample.MyConfig.file=/etc/example.conf

Tamaya default key resolution

The above keys hereby are the fully keys Tamaya tries to resolve first. If no entry for the fully qualified key has been found Tamaya also looks up further keys by removing the preceding parts:

MyConfig.port=8088
MyConfig.name=http
MyConfig.file=/etc/example.conf

And finally:

port=8088
name=http
file=/etc/example.conf

Defining keys explicitly

The keys to be used for identifying configuration can be explicitly annotated as well:

@ConfigProperty("app-port")
private int port;
@ConfigProperty("[suite.app-port-name]")
private String name;

Hereby

  • The first key is interpreted as the relative key. Tamaya will still do the default key resolution logic, but uses the defined key as the final element in the key path.
  • The second key is interpreted as a global key, which will prevent default key resolution. So Tamaya will only lookup the given key (without the brackets, of course).

Finally Tamaya also allows to define multiple keys, where relative and absolute keys can be mixed arbitrarely. The list of key candidates hereby is assembled as declared in the annotation, e.g.

@ConfigProperty("[suite.app-port-name]", "app-port")

will be resolved to the following key candiadate list:

suite.app-port-name
configsample.MyConfig.app-port
MyConfig.app-port
app-port

Setting the root section explictly

Finally somethimes you want to prevent Tamaya’s key resolution, but would like to set a root path to be used in combination with your relative entries. This can be achieved by using the @ConfigSection annotation:

@ConfigSection("cluster")
public final class MyConfig{
  @ConfigProperty("app-port")
  private int port;
  ...

The above annotation will result in the following key candiadate list:

cluster.app-port
app-port

Defining default values

Tamaya also supports defining default values for configuration entries by using annotations. Hereby the default values can be any String that can be converted by a registered PropertyConverter for the required target type:

@ConfigProperty(defaultValue="8088")
private int port;
@ConfigProperty(defaultValue="http")
private String name;

Preventing injection

If some properties are not targeting for configuration injection these can be annotated as well:

@NoConfig
private String notToBeConfigured;

Summary

As we have seen Tamaya supports customizing the property key resolution in a very flexible way. There would be several additional things to be uncovered, which will be discussed in future posts.

Advertisements

About atsticks

Advanced Java Software Engineer and Architect.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s