Skip to content

Typesafe, container ready configurations for java

Notifications You must be signed in to change notification settings

GaruGaru/confy2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Confy

Typesafe, container ready configurations for java

Build Status codecov

Usage

Define configuration interface

    public interface MyConfiguration {
    
        @Param.String() // Key from method name
        String getHost();
    
        @Param.Integer(key = "port", defaultValue = 0)
        int getPort();
    
        @Param.Float(key = "threshold", defaultValue = 0.5F)
        float getThreshold();
    
    }

Make Confy implements methods (by default from env. and configuration.properties if present)

    MyConfiguration conf = Confy.implement(MyConfiguration.class);
    String host = conf.getHost();
    int port = conf.getPort();

Custom loading

    MyConfiguration conf = Confy.fromArgs(args)
                .withProperty("myconf.properties")
                .withEnv()
                .to(MyConfiguration.class)
    String host = conf.getHost();
    int port = conf.getPort();

Keys resolution using method names

    public interface DatabaseConfiguration {
    
        @Param.String() 
        String getDatabaseHost(); // Env: DATABASE_HOST | Property: database.host

        @Param.Integer(defaultValue = 3306)
        int getDatabasePort(); // Env: DATABASE_PORT| Property: database.port
        
    }

Configurations priority examples

Args > Environment > Property
    MyConfiguration conf = Confy.create()
                .withProperty("myconf.properties")
                .withEnv()
                .withArgs(args)
                .to(MyConfiguration.class)
Environment > Args > Property
    MyConfiguration conf = Confy.create()
                .withProperty("myconf.properties")
                .withArgs(args)
                .withEnv()
                .to(MyConfiguration.class)

Use the same keys between env and properties!

test.properties

my.env=confy

environment

MY_ENV=confy-env

Get using same key

confy.get("my.env"): "confy-env"

Installation

Add jitpack repository
	<repositories>
		<repository>
		    <id>jitpack.io</id>
		    <url>https://jitpack.io</url>
		</repository>
	</repositories>
Add dependency
	<dependency>
	    <groupId>com.github.GaruGaru</groupId>
	    <artifactId>confy2</artifactId>
	    <version>1.3</version>
	</dependency>