Guice Configuration

Guice configuration module to inject configuration from files as JSON or HOCON format

License

License

Categories

Categories

Net GUI User Interface Guice Application Layer Libs Dependency Injection
GroupId

GroupId

net.jmob
ArtifactId

ArtifactId

guice.conf
Last Version

Last Version

1.4.3
Release Date

Release Date

Type

Type

jar
Description

Description

Guice Configuration
Guice configuration module to inject configuration from files as JSON or HOCON format
Project URL

Project URL

https://github.com/yyvess/gconf
Source Code Management

Source Code Management

https://github.com/yyvess/gconf.git

Download guice.conf

How to add to project

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

Dependencies

compile (5)

Group / Artifact Type Version
com.google.inject : guice jar 4.2.3
com.google.inject.extensions : guice-assistedinject jar 4.2.3
ch.qos.logback : logback-classic jar 1.2.3
com.typesafe : config jar 1.4.1
javax.validation : validation-api jar 2.0.1.Final

test (4)

Group / Artifact Type Version
junit : junit jar 4.13.1
org.mockito : mockito-core jar 3.6.28
org.hibernate.validator : hibernate-validator-cdi jar 6.1.6.Final
org.glassfish : javax.el jar 3.0.0

Project Modules

There are no modules declared in this project.

Guice configuration module, JSON, HOCON & Properties formats supported, build on the top of Typesafe config

Maven Central

Sonar Status Coverage

Guice configuration

Overview

  • Guice injection
  • JSON, HOCON and Properties formats
  • Substitutions ${foo.bar}
  • Validation

Binary Releases

You can find published releases on Maven Central.

	<dependency>
		<groupId>net.jmob</groupId>
		<artifactId>guice.conf</artifactId>
		<version>1.4.3</version>
	</dependency>

Optionally, to active validation, you must import a validator like Hibernate validator

    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator-cdi</artifactId>
        <version>6.0.15.Final</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.0</version>
    </dependency>

Link for direct download if you don't use a dependency manager:

Quickstart

A configuration file app.json :

{
  "port": 8080,
  "complexEntries": {
    "hostname": "www.github.com",
    "aMap": {
      "key1": "value1",
      "key2": "value2"
    },
    "aList": [
      "value1",
      "value2"
    ]
  }
}

An interface where inject your structured configuration

   public interface MyServiceConfiguration {

      @Length(min = 5)
      String getHostname();
    
      Map<String, String> getAMap();
    
      List<String> getAList();
   }

A service where configuration should be inject

    @BindConfig(value = "app", syntax = JSON)
    public class Service {

        @InjectConfig
        private Optional<Integer> port;

        @InjectConfig("complexEntries")
        private MyServiceConfiguration config;

        public int getPort() {
            return port.orElse(0);
        }

        public ServiceConfiguration getConfig() {
            return config;
        }
    }
    public class GuiceModule extends AbstractModule {
        @Override
        protected void configure() {
            install(new ConfigurationModule());
            requestInjection(Service.class);
        }
    }

Configuration files are loaded of classpath by default

A directory can be specified to load configuration outside of classpath

    public class GuiceModule extends AbstractModule {
        @Override
        protected void configure() {
            install(new ConfigurationModule()
                .fromPath(new File("/etc")));
            requestInjection(Service.class);
        }
    }

Variables on your configuration file can be substitued with environment variables.

Substitution should be active with the option 'resolve'

@BindConfig(value = "config, resolve = true)
{
  myconfig: ${my.environement.property}
}

Please find more examples on src/test/samples

Supported types

  • boolean, Boolean
  • String
  • int, Integer, double, Double
  • List, Map, with typed value support
  • Optional<?>
  • Any Interface, a proxy of this interface is injected

References

License

The license is Apache 2.0, see LICENSE file.

Copyright (c) 2015-2016, Yves Galante

Versions

Version
1.4.3
1.4.2
1.4.1
1.4.0
1.3.0
1.2.0
1.1.0
1.0.0
v1.5.0