configuration

A configuration library for Java applications.

License

License

Categories

Categories

Configuration Application Layer Libs config
GroupId

GroupId

com.github.srbarber1997
ArtifactId

ArtifactId

configuration
Last Version

Last Version

1.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

configuration
A configuration library for Java applications.
Project URL

Project URL

https://github.com/srbarber1997/configuration
Source Code Management

Source Code Management

https://github.com/srbarber1997/configuration/tree/master

Download configuration

How to add to project

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

Dependencies

compile (4)

Group / Artifact Type Version
org.reflections : reflections jar 0.9.11
com.google.code.gson : gson jar 2.3.1
com.google.guava : guava jar 23.0
org.slf4j : slf4j-api jar 1.7.24

test (2)

Group / Artifact Type Version
org.mockito : mockito-core jar 1.10.19
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

Configuration

A configuration library for Java applications. Including support for SpringBoot apps. Easily define configs and load them into any class.

The use of Configuration is similar to a database, except it is much more lightweight. This should not be used instead of a database.
The package provides a way of retaining data between runtime executions. Normally, if you save a value to a variable during runtime, the next time you run the jar it's not there. With this package you could be.

Basic Use

First, define your configuration model class
It can be done inside another class but it is not advised

@ConfigurationModel
public class Config {
    private int number;
    // ...
    
    public int getNumber() {
        return number;
    }
    // ...
}

Then, add your config instance variable to your class of choice, specify the name of the config. Naming the configuration, uniquely identifies it (behind the scenes it's the file that is generated, containing a json representation of the object). So "config" would be "{configDirectory}/config.config"

public class App {
    @Configuration(name = "config")
    private static Config config;
}

Make sure to use the load() method on the ConfigLoader class to instantiate the @Configuration variables for you

public class App {
    public static void main(String[] args) {
        // Run the loader to instantiate instance variables
        ConfigLoader.load();
        // This only has to be done once
        
        // Then you can access the variable...
        System.out.println(config.getNumber());
    }
}

The ConfigLoader.load() method does not just load the variables in the class you run it in. You can run the method anywhere and all variables annotated with @Configuration will be loaded or, if they do not already exist, created.

Save the changes you've made using this method, again, like with the load() method this works for the entire classpath. This method is not needed. A Shutdown Hook is registered so a configs are saved on shutdown

public class App {
    public static void main(String[] args) {
        ConfigLoader.load();
        // Change config variable
        ConfigLoader.save();
    }
}

Advanced Use

Modifiers

Add modifiers to the configuration, they will be applied to it when loading and saving.
For example, make the config not readable

public class App {
    @Configuration(name = "config", readable = false)
    private static Config config;
}

Default Configurations

You can specify a default configuration for a model. This will be used if there isn't one saved. This will overwrite the default values specified in the class.

@ConfigurationModel(defaultResource = "myConfigDefault.config")
public class MyConfig {
    
}

The path given will look for a file within the resources root. The contents of the default resource file should be a json representation of the configuration that will be loaded. The default resource must be "readable"

Configuring the Serializer

If you require a specific way of serialising and de-serialising an object you will have to tell the loader how. In order to do this you will have to implement an interface so the loader knows how to serialize your model

@ConfigurationModel
public class MyConfig implements SelfSerializable {
    @Override
    public String serialise(Object obj) throws Exception {
        // obj == this; however you should always use 'obj' the serialise not 'this'
        // return the string representation of the given object
    }
}

The same is required to deserialise, however if you are using json you may omit this as by default the Config Loader deserialises an object using Gson.

@ConfigurationModel
public class MyConfig implements SelfSerializable {
    @Override
    public Object deserialize(String objString, Class<?> objClass) throws Exception {
        // return the object that is represented by the given string    
    }
}

Each model provide it's own in order to protect multiple models wanting different ways of serializing the same type of object.

Adding Descriptions

If your Config Model requires editing via the config directory. In other words, the user editing the .config json file. Then it might be useful to have a description of what values are acceptable.

Spring Boot

This library can be used in conjunction with Spring Boot. The additional module provides a PostConstruct method to automatically load the configs on startup.

compile 'org.bitbucket.srbarber1997:configuration-spring:+'
<dependency>
    <groupId>org.bitbucket.srbarber1997</groupId>
    <artifactid>configuration-spring</artifactid>
</dependency>

Configure

When using Spring Boot you can configure how the Config Loader loads. Add the options to the application.properties or application.yml

configuration.auto-load=false
configuration.log-output=false
configuration:
    auto-load: false
    log-output: false

Both options default to true if they are not present.

Versions

Version
1.0.1