Java EE application configuration using Aeonbits Owner

Every (almost) Java EE based application needs configuration — local, test or production — it does not matter. What matters — how easily this configuration solution is maintained and how easily the environment may be changed.

There are many ways to achieve the requirement, however in this blog I will cover one of possible CDI based configuration solutions — Owner by http://owner.aeonbits.org/.

Usually there are several stages when properties used in production:

  1. Hardcoded properties (usually done by novice programmers, otherwise I would treat such properties as constants);
  2. Properties file inside application war file (different properties configured during build time. It is not correct, because the same build cannot be deployed on different environments);
  3. Properties file stored in server file system (properties are independent from war and is environment specific).

Java EE lacks easy configuration functionality. There are many discussions related to this topic

  1. HOW TO CONFIGURE JAVA EE 6+ APPLICATIONS …WITHOUT XML
  2. Storing and editing configuration for Java EE applications at Stack Overflow
  3. Configure Java EE applications or “Putting Bien into practice” and more others.

However they need custom actions to take into account:

  • You need to manage property injection yourself;
  • You need to use Spring framework, which has flexible configuration management mechanism. But do you need Spring only for property loading? I believe — no ☺

Preconditions

You need this environment setup in order to successfully complete the provided guide:

Implementation

Defining an interface

/**
 * Application configuration properties are defined here.
 * 
 * @author vicento.ramos
 *
 */
@HotReload
@Sources({
“file:/etc/my-app.properties”, “file:~/.my-app.properties”, “classpath:/my-app.properties” })
@Named(“applicationConfig”)
public interface ApplicationConfig extends Accessible {
 @DefaultValue(“50”)
 @Key(“app.timeout”)
 int getInitialTimeout();
}

Creating a producer

/**
 * Application scope based configuration file producer.
 * 
 * @author vicento.ramos
 *
 */
@ApplicationScoped
public class ApplicationConfigProducer {
 @Produces
 @ApplicationScoped
 public ApplicationConfig produceSportsConfig() {
  return ConfigFactory.create(ApplicationConfig.class);
 }
}

Creating a properties file

my-app.properties file may be stored in one of these locations:

  • /etc/my-app.properties
  • .my-app.properties
  • /my-app.properties

And the content of the file:

app.timeout=30

Usage

/**
 * Sample injection point of properties.
 * 
 * @author
vicento.ramos
 *
 */
@Named
public class SampleService {
 @Inject
 private ApplicationConfig applicationConfig;
 
 public void method() {
  System.out.println(String.format(“Config value: %d”, 
  applicationConfig.getInitialTimeout()));
 }
}

Features

  • Multiple configuration locations — you may configure multiple lookup locations where configuration files may be fetched from;
  • CDI Injection — Java EE based CDI injection using @Named and @Inject annotation
  • Default values — you may configure default values easily using @DefaultValue
  • Hot reload — you may easily define the interval for properties hot reloading using @HotReload
  • And many more if required

Resources

vytas

Senior software engineer, software architect for java enterprise based solutions