Injecting external properties in Java EE 6/7 applications

My first post is about a project I started a few weeks ago after reading a good post from Markus Eisele (here) about injecting external properties into a Java EE 6/7 project with CDI enabled.
External means that the properties files are not packaged inside the war/ear file but instead deployed separately and accessible to the application’s code through its classpath, this is Application Server specific and will not be covered here.

How to generate the jar

The project is available on gitub as source code only but it’s easy to generate the jar file

Requirements

  • Java 1.7
  • Maven 3
  • Git (only needed if you want to clone the project)

Build the project

  • Clone the project (you can also download it as a zip file and import it in your IDE)
  • Type the maven command below to generate the jar file and add it to your local maven repository
    mvn clean install
  • Add the jar to your project dependencies, if you use maven add this snippet to your pom.xml
    		<dependency>
    			<groupId>com.github.djallalserradji</groupId>
    			<artifactId>inject.property</artifactId>
    			<version>${version_you_cloned}</version>
    		</dependency>
    

How it works

Let’s say we have a billing class that needs 3 web services, a Billing web service to process billing information received from customers, an email web service to notify the customer that the billing happened and a SMS web service to notify the customer on their mobile phone if they choose to.

public class BillingService {
	
	private String billingEndpoint;
	private String emailEndpoint;
	private String smsEndpoint;

	// Business logic
	// .........
}

And we want to inject the endpoint URLs stored in the external properties file billing.properties

billing.endpoint=http://billing.company.com
email.endpoint=http://email.company.com
sms.endpoint=http://sms.company.com

First annotate the class with @PropertiesFiles like this

@PropertiesFiles({"billing.properties"})
public class BillingService {

The annotation takes a list of files (coma separated)

Then annotate each field which will receive the value of a property with @Property

@PropertiesFiles({"billing.properties"})
public class BillingService {
	
	@Property("billing.endpoint")
	@Inject
	private String billingEndpoint;
	
	@Property("email.endpoint")
	@Inject
	private String emailEndpoint;
	
	@Property("sms.endpoint")
	@Inject
	private String smsEndpoint;
	
	// Business logic
	// .........
}

@Property takes a single parameter that represents the name of the property
Please note that @Inject annotation is mandatory in order for the injection to happen.

That’s it!

Alternatively, if you don’t want to use the class annotation @PropertiesFiles you can list all the properties files in a single configuration file named inject.property.xml (they will be available to all classes), this file must be available in the classpath (either packaged inside the war/ear or externally available) and has this structure:

<propertiesfiles>
	<file>file1.properties</file>
	<file>file2.properties</file>
	<file>...</file>
</propertiesfiles>

Important:

If a class is annotated with @PropertiesFiles the configuration file is ignored

The source code is available on gitub

Thank you for reading.
Your comments are welcome.