Introduction

This is an extension of smartics-properties to support injecting property sets with CDI. Right now only Weld is supported.

If you want to define properties, please refer to smartics-properties-config.

Having the properties declared and defined you can - using this extension - inject them using CDI.

Quickstart

Configuration

If you are using Maven, add the following dependencies to your pom.xml:

<dependency>
  <groupId>de.smartics.properties</groupId>
  <artifactId>smartics-properties-integration-cdi</artifactId>
  <version>0.1.3-SNAPSHOT</version>
</dependency>

Having added the dependency it is also necessary to specify the groupId, artifactId and version that will be used to lookup the properties. One way to provide this infromation is by copying the artifact coordinates to the manifest.mf file of your archive or by providing them manually by using the following keys:

Implementation-Vendor-Id: ${project.groupId}
Implementation-Title: ${project.artifactId}
Implementation-Version: ${project.version}

The following maven-plugins can help you putting this information in the manifest.mf:

An example for the maven-ear-plugin:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-ear-plugin</artifactId>
  <version>2.3.2</version>
  <configuration>
    ...
    ...
    <archive>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
        <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
      </manifest>
    </archive>
  </configuration>
</plugin>

An example for the maven-ear-plugin manually configuring the values:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-ear-plugin</artifactId>
  <version>2.3.2</version>
  <configuration>
    ...
    ...
    <archive>
      <manifestEntries>
        <Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
        <Implementation-Title>${project.artifactId}</Implementation-Title>
        <Implementation-Version>${project.version}</Implementation-Version>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>

Last but not least the node and environment must be set. This can be done by using the following java system properties in JAVA_OPTS:

  • properties.environment.node
  • properties.environment.name

e.g. Windows:

set "JAVA_OPTS=%JAVA_OPTS% -Dproperties.environment.node=node1 -Dproperties.environment.name=production"

e.g. Linux:

JAVA_OPTS="$JAVA_OPTS -Dproperties.environment.node=node2 -Dproperties.environment.name=preproduction"

Usage / Injection

Now comes the simple part: Injecting the properties / using one.

package de.smartics.sandbox;
import javax.ejb.Stateless;
import javax.inject.Inject;

import de.smartics.sandbox.ApplicationProperties;

@Stateless
public class SimplePropertiesService {
  @Inject
  private ApplicationProperties appProperties;

  /**
   * Do something.
   */
  public void doSomething() {
    final String url = appProperties.url();
    ...
  }
}

ApplicationProperties is an interface defining the property keys as shown in smartics-properties' 5 Minutes Tutorial.