Code Monkey home page Code Monkey logo

guice-configuration's Introduction

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

guice-configuration's People

Contributors

the-alchemist avatar yannickg-msi avatar yyvess avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.