Skip to content

yyvess/Guice-configuration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Maven Central semantic-release

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>v1.5.0</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.2.0.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

About

Guice configuration module allows inject values from files as JSON, HOCON and Properties format

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages