Git Product home page Git Product logo

auto-config's Introduction

Auto-Config

Auto-Config is an easy, lightweight and simple to use way to create object-related configurations in many different formats. It is meant to be used with the dependency framework guice by google but also supports standalone usage.

Supported Formats:

Auto-Config supports at the time the following configuration formats:

Format Implemented with
JSON Gson
Yaml In Development

Examples with Guice

ExampleConfiguration:

@Singleton //Used to mark this class as a Singleton for Guice.
@Configuration(path = "example.json", type = JsonConfigurationType.class)
public class ExampleConfig extends ConfigurationAccessor {

  private final transient Injector injector; //Any type of dependency injection is possible here.
  private transient String test; //transient fields will be ignored
  private String name;

  @Inject
  private ExampleConfig(Injector injector) {
    this.injector = injector;
  }


  public void loadDefaults() {
    name = "Jan Brachthäuser";
    test = "Test";
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

Usage:

public class GuiceTest {

  private final ExampleConfig exampleConfig;

  @Inject
  public GuiceTest(ExampleConfig exampleConfig) {
      System.out.println(exampleConfiguration.getName()); //Will return 'Jan Brachthäuser' by default.

    exampleConfiguration.setName("Melvin");
    exampleConfiguration.save(); //Will save the configuration in the given format to the file specified in @Configuration.

    System.out.println(exampleConfiguration.getName()); //Will return 'Melvin' now.
  }

  public static void main(String[] args) {
    Guice.createInjector().getInstance(GuiceTest.class);
  }
}

Examples without Guice:

ExampleConfiguration:

@Configuration(path = "example.json", type = JsonConfigurationType.class)
public class ExampleConfig extends ConfigurationAccessor {

  private transient String test; //transient fields will be ignored
  private String name;


  public void loadDefaults() {
    name = "Jan Brachthäuser";
    test = "Test";
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}

Usage:

public final class NoGuiceTest {

  private ExampleConfig exampleConfiguration;

  private NoGuiceTest() {
    this.exampleConfiguration = new ExampleConfig();
    this.init();
  }

  private void init() {
    System.out.println(exampleConfiguration.getName()); //Will return 'Jan Brachthäuser' by default.

    exampleConfiguration.setName("Melvin");
    exampleConfiguration.save(); //Will save the configuration in the given format to the file specified in @Configuration.

    System.out.println(exampleConfiguration.getName()); //Will return 'Melvin' now.
  }


  public static void main(String[] args) {
    new NoGuiceTest();
  }
}

auto-config's People

Contributors

jan-br avatar

Stargazers

 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.