Friday 20 May 2016

Java: Owner: importing properties from properties instance

When you looks at the create method declaration in ConfigFactory interface, it looks like below.

public static <T extends Config> T create(Class<? extends T> clazz, Map<?, ?>... imports)


We can pass number of maps to create method to create an instance from the specified interface.
package owner_api_tutorial;

import org.aeonbits.owner.Config;

public interface ProjectConfig extends Config {

  @Key("images.directory")
  String imagesDirectory();

  @Key("images.backup.directory")
  String imagesBackupDirectory();

  @Key("videos.directory")
  String videosDirectory();

  @DefaultValue("16")
  int maxIcons();

  @DefaultValue("admin@admin.com")
  String adminMail();
}

package owner_api_tutorial;

import org.aeonbits.owner.ConfigFactory;
import java.util.*;

public class PropertyUtil {
  public static void main(String args[]) throws Exception {
    Map<String, String> properties1 = new HashMap<>();
    Map<String, String> properties2 = new HashMap<>();

    properties1.put("videos.directory", "/Users/harikrishna_gurram/videos");
    properties1.put("maxIcons", "9");

    properties2.put("images.directory", "/Users/harikrishna_gurram/images");
    properties2.put("images.backup.directory",
        "/Users/harikrishna_gurram/backup/images");
    properties2.put("maxIcons", "19");

    ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class,
        properties1, properties2);

    System.out.println(cfg.adminMail());
    System.out.println(cfg.imagesDirectory());
    System.out.println(cfg.imagesBackupDirectory());
    System.out.println(cfg.maxIcons());
    System.out.println(cfg.videosDirectory());

  }
}


Output
admin@admin.com
/Users/harikrishna_gurram/images
/Users/harikrishna_gurram/backup/images
9
/Users/harikrishna_gurram/videos


What happens if two Map instances has same property?
First map takes precedence over second map. For example, in the above example, both the maps properties1, properties2 has the property maxIcons, since create method called with arguments properties1 followed by properties2 ("ConfigFactory.create(ProjectConfig.class,properties1, properties2);"), maxIcons has the value 9.

properties1.put("maxIcons", "9");
properties2.put("maxIcons", "19");

What happen if my properties loaded using @Source annotation and maps are same?
Properties imported programmatically have higher priority regarding the properties loaded from the @Sources attribute.

App.properties

#Project configurations
videos.directory = /App/videos
maxIcons = 9
version = "9.8"


Con.properties

#Project configurations
images.directory = /Con/images
images.backup.directory = /Con/backup/images
maxIcons = 19


ProjectConfig.java

package owner_api_tutorial;

import org.aeonbits.owner.Config;
import org.aeonbits.owner.Config.Sources;
import org.aeonbits.owner.Config.LoadPolicy;
import org.aeonbits.owner.Config.LoadType;

@LoadPolicy(LoadType.MERGE)
@Sources({ "file:~/.myapp.config", 
  "classpath:App.properties", 
    "classpath:Con.properties" })
public interface ProjectConfig extends Config {

  @Key("images.directory")
  String imagesDirectory();

  @Key("images.backup.directory")
  String imagesBackupDirectory();

  @Key("videos.directory")
  String videosDirectory();

  @DefaultValue("16")
  int maxIcons();

  @DefaultValue("admin@admin.com")
  String adminMail();
  
  @DefaultValue("10")
  String version();
}


PropertyUtil.java

package owner_api_tutorial;

import org.aeonbits.owner.ConfigFactory;
import java.util.*;

public class PropertyUtil {
  public static void main(String args[]) throws Exception {
    Map<String, String> properties1 = new HashMap<>();
    Map<String, String> properties2 = new HashMap<>();

    properties1.put("videos.directory", "/Users/harikrishna_gurram/videos");
    properties1.put("maxIcons", "9");

    properties2.put("images.directory", "/Users/harikrishna_gurram/images");
    properties2.put("images.backup.directory",
        "/Users/harikrishna_gurram/backup/images");
    properties2.put("maxIcons", "19");

    ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class,
        properties1, properties2);

    System.out.println(cfg.adminMail());
    System.out.println(cfg.imagesDirectory());
    System.out.println(cfg.imagesBackupDirectory());
    System.out.println(cfg.maxIcons());
    System.out.println(cfg.videosDirectory());
    System.out.println(cfg.version());

  }
}


Output

admin@admin.com
/Users/harikrishna_gurram/images
/Users/harikrishna_gurram/backup/images
9
/Users/harikrishna_gurram/videos
"9.8"


Previous                                                 Next                                                 Home

No comments:

Post a Comment