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();

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/, so lets add the following properties:


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:


And finally:


Defining keys explicitly

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

private int port;
private String name;


  • 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("[]", "app-port")

will be resolved to the following key candiadate list:

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:

public final class MyConfig{
  private int port;

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

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:

private int port;
private String name;

Preventing injection

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

private String notToBeConfigured;


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.


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: Logo

You are commenting using your 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