Guice Commons Configuration

A Guice module using Apache Commons Configuration for mapping named properties by using a type-based injection.

License

License

Categories

Categories

Net GUI User Interface Configuration Application Layer Libs config Guice Dependency Injection
GroupId

GroupId

net.lamberto
ArtifactId

ArtifactId

guice-commons-configuration
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Guice Commons Configuration
A Guice module using Apache Commons Configuration for mapping named properties by using a type-based injection.
Project URL

Project URL

https://github.com/marcolamberto/guice-commons-configuration
Source Code Management

Source Code Management

https://github.com/marcolamberto/guice-commons-configuration.git

Download guice-commons-configuration

How to add to project

<!-- https://jarcasting.com/artifacts/net.lamberto/guice-commons-configuration/ -->
<dependency>
    <groupId>net.lamberto</groupId>
    <artifactId>guice-commons-configuration</artifactId>
    <version>1.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/net.lamberto/guice-commons-configuration/
implementation 'net.lamberto:guice-commons-configuration:1.0.1'
// https://jarcasting.com/artifacts/net.lamberto/guice-commons-configuration/
implementation ("net.lamberto:guice-commons-configuration:1.0.1")
'net.lamberto:guice-commons-configuration:jar:1.0.1'
<dependency org="net.lamberto" name="guice-commons-configuration" rev="1.0.1">
  <artifact name="guice-commons-configuration" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.lamberto', module='guice-commons-configuration', version='1.0.1')
)
libraryDependencies += "net.lamberto" % "guice-commons-configuration" % "1.0.1"
[net.lamberto/guice-commons-configuration "1.0.1"]

Dependencies

compile (8)

Group / Artifact Type Version
org.projectlombok : lombok jar 1.16.18
commons-configuration : commons-configuration jar 1.10
com.google.guava : guava jar 23.0
com.google.inject : guice jar 4.1.0
org.slf4j : slf4j-api jar 1.7.12
ch.qos.logback : logback-classic jar 1.1.3
ch.qos.logback : logback-core jar 1.1.3
junit : junit jar 4.12

test (3)

Group / Artifact Type Version
net.lamberto.junit : guice-junit-runner jar 1.0.2
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3

Project Modules

There are no modules declared in this project.

guice-commons-configuration

Continuous Integration: Build Status

A simple way for injecting typed configuration properties.

What is guice-commons-configuration?

guice-commons-configuration is a Guice module using Apache Commons Configuration for mapping named properties by using a type-based injection.

Basic usage

Configuration options can be placed in classes or interfaces. A configuration option could simply extend an existing type from ConfigurationOptionTypes.

public static final class HOSTNAME extends ConfigurationOptionTypes.StringOption {}
public static final class PORT extends ConfigurationOptionTypes.IntegerOption {}

You could easily create your own typed option by implementing the ConfigurationOptionType interface.

public class StructuredOptions {
	// ...
	
	public static StructuredOptions valueOf(String data) {
		// ... impl goes here
	}
	
	// ...
}

public static final class CUSTOM_OPTION implements ConfigurationOptionType<StructuredOptions> {
	@Override
	public Class<StructuredOptions> getConfigurationType() {
		return StructuredOptions.class;
	}

	@Override
	public StructuredOptions getValueFor(final String name, final Configuration configuration) {
		// ... building custom class instance
		return StructuredOptions.valueOf(configuration.getProperty(name));
	}
}

The Guice module requires a Map<String, String> with the configuration values and a variable list of configuration options or classes and interfaces holding them. Each key in the Map is named accordingly to the configuration option class.

Every injected configuration option must be present in the configuration Map.

You could install as many ConfigurationOptionsModule as you like but you have to pay attention to the configuration option classes because Guice bindings have to be unique.

// ... within a Module
// optionsMap = { HOSTNAME="github.com", PORT="80", CUSTOM_OPTION="{x:1,y:2,z:3}" }
install(new ConfigurationOptionsModule(
	optionsMap,
	HOSTNAME.class,
	PORT.class,
	AnotherClassOrInterfaceHoldingOptions.class
);
//... 

Injection requires the ConfigurationOption annotation with the configuration option class as value.

public MyClass {
	@Inject @ConfigurationOption(HOSTNAME.class)
	private String hostName;

	@Inject @ConfigurationOption(PORT.class)
	private Integer port;
	
	@Inject @ConfigurationOption(CUSTOM_OPTION.class)
	private StructuredOptions options;
}

Versions

Version
1.0.1
1.0.0