Configure with CDI

Configuration using CDI

License

License

Categories

Categories

config Application Layer Libs Configuration
GroupId

GroupId

org.honton.chas
ArtifactId

ArtifactId

config-cdi
Last Version

Last Version

0.1.2
Release Date

Release Date

Type

Type

jar
Description

Description

Configure with CDI
Configuration using CDI
Project URL

Project URL

https://github.com/chonton/config-cdi
Source Code Management

Source Code Management

https://github.com/chonton/config-cdi

Download config-cdi

How to add to project

<!-- https://jarcasting.com/artifacts/org.honton.chas/config-cdi/ -->
<dependency>
    <groupId>org.honton.chas</groupId>
    <artifactId>config-cdi</artifactId>
    <version>0.1.2</version>
</dependency>
// https://jarcasting.com/artifacts/org.honton.chas/config-cdi/
implementation 'org.honton.chas:config-cdi:0.1.2'
// https://jarcasting.com/artifacts/org.honton.chas/config-cdi/
implementation ("org.honton.chas:config-cdi:0.1.2")
'org.honton.chas:config-cdi:jar:0.1.2'
<dependency org="org.honton.chas" name="config-cdi" rev="0.1.2">
  <artifact name="config-cdi" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.honton.chas', module='config-cdi', version='0.1.2')
)
libraryDependencies += "org.honton.chas" % "config-cdi" % "0.1.2"
[org.honton.chas/config-cdi "0.1.2"]

Dependencies

compile (8)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.9.8
com.fasterxml.jackson.dataformat : jackson-dataformat-xml Optional jar 2.9.8
com.fasterxml.jackson.dataformat : jackson-dataformat-yaml Optional jar 2.9.8
com.fasterxml.jackson.module : jackson-module-mrbean Optional jar 2.9.8
com.jasonclawson : jackson-dataformat-hocon Optional jar 1.1.0
javax.enterprise : cdi-api jar 1.2
javax.inject : javax.inject jar 1
org.slf4j : slf4j-api jar 1.7.21

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.8

test (3)

Group / Artifact Type Version
junit : junit jar 4.12
org.jglue.cdi-unit : cdi-unit jar 3.1.3
org.mockito : mockito-core jar 1.10.19

Project Modules

There are no modules declared in this project.

Use CDI to inject application configuration.

ConfigurationCache is a generic class that reads configuration documents and fills pojos (Plain Old Java Objects) or reifies pojis (Plain Old Java Interfaces) with the parsed values. With this library CDI will inject ConfigurationCache sites marked with @Inject and @ConfigurationSource. The configuration source is read from the location specified in the @ConfigurationSource annotation.

public class MyClassThatNeedsConfiguration {

	@Inject @ConfigurationSource("classpath:conf-config-bean.conf")
	private ConfigurationCache<ConfigBean> configCache;
    
    public String doSomethingThatNeedsConfiguration() {
    	ConfigBean config = configCache.get();
        // use values from config
        // ...
	}
}

Features

  • Thread safe
  • Multiple configuration source document formats
  • Automatic reload of configuration values when document changes
  • Configuration set consistency
  • Annotation driven document source
  • Annotation driven document to object mapping
  • Configuration Interfaces as well as Objects

@ConfigurationSource elements

ConfigurationSource has two elements:

  • value - URI to locate the configuration source document. The file extension is used to determine the document data format.
  • interval - Number of milliseconds before refetching source document

Supported Data Formats

Configuration-cdi supports the following source file formats:

Single document to multiple configuration objects

It is possible to have a single document specified by multiple ConfigurationCache injection points. Each resultant object will be filled with its subset of attributes defined in the document.

Document per configuration object

It is also possible to have a document specified for each ConfigurationCache injection point.

Extra attributes

Attributes specified in the document without a corresponding configuration object attribute are ignored.

Missing attributes

Configuration object attributes without a corresponding attribute in the document are left as initialized by java.

Add to your maven project

<dependency>
  <groupId>org.honton.chas</groupId>
  <artifactId>config-cdi</artifactId>
  <version>0.1.2</version>
</dependency>

Add optional support for configuration interfaces

<dependency>
  <groupId>com.fasterxml.jackson.module</groupId>
  <artifactId>jackson-module-mrbean</artifactId>
  <version>2.7.4</version>
</dependency>

Add optional support for Yaml

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-yaml</artifactId>
  <version>2.9.8</version>
</dependency>

Add optional support for Conf

<dependency>
  <groupId>com.jasonclawson</groupId>
  <artifactId>jackson-dataformat-hocon</artifactId>
  <version>1.1.1</version>
</dependency>

Add optional support for Xml

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
  <version>2.9.8</version>
</dependency>

Mapping Annotations

Jackson is used to parse documents and populate the configuration pojos. All of the standard Jackson annotations can be used to direct the document to object mapping.

Versions

Version
0.1.2
0.1.0