GithubHelp home page GithubHelp logo

davidopdebeeck / guice-external-config Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 66 KB

A simple guice module library to import external configuration files as java objects.

License: MIT License

Java 100.00%
guice configuration

guice-external-config's Introduction

Guice external config

A simple guice module library to import external configuration files as java objects.

Getting started

  1. Register the module with the Guice injector.
// Scan all packages for classes annotated with @Configuration
Injector injector = createInjector(new ExternalConfigModule())
// Scan only 'com.company' for classes annotated with @Configuration
Injector injector = createInjector(new ExternalConfigModule("com.company"))
  1. Create a json file with your configuration.
{
  "applicationName": "<string>",
  "listOfStrings": ["<string>", "<string>"],
  "complexObject": {
    "stringField" : "<string>"
  },
  "listOfComplexObjects": [{
      "stringField" : "<string>"
  }]
}
  1. Create a java configuration class annotated with @Configuration.
    • location - the relative path to the json file
    • external - if the configuration file is inside (false) or outside (true) the jar

Note: Configuration classes need an empty constructor (can be private or an implicit public one) and need a getter for each configuration property.

Note: Configuration properties that are present in the json file but not in the java class will simply not be mapped (the application will not fail).

@Configuration(location = "application.json", external = true)
public class ApplicationConfig {

    @SuppressWarnings("unused")
    private String applicationName;
    @SuppressWarnings("unused")
    private List<String> listOfStrings;
    @SuppressWarnings("unused")
    private ComplexObject complexObject;
    @SuppressWarnings("unused")
    private List<ComplexObject> listOfComplexObjects;

    public String getApplicationName() {
        return applicationName;
    }

    public List<String> getListOfStrings() {
        return listOfStrings;
    }

    public ComplexObject getComplexObject() {
        return complexObject;
    }

    public List<ComplexObject> getListOfComplexObjects() {
        return listOfComplexObjects;
    }
}
public class ComplexObject {

    private String stringField;

    public String getStringField() {
        return stringField;
    }
}

guice-external-config's People

Contributors

davidopdebeeck avatar

Watchers

 avatar  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.