Friday 20 May 2016

Java: owner: Customize property keys


Usually the longer properties names are defined using ‘.’ operator like below.

ProjectConfig.properties
#Project configurations
images.directory = /Users/harikrishna_gurram/images
images.backup.directory = /Users/harikrishna_gurram/backup/images
videos.directory = /Users/harikrishna_gurram/videos
maxIcons = 9

How can I map these longer property names to my ProjectConfig interface?

Owner API provides @Key annotation, by using this we can map a property name to the associated method.
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;

public class PropertyUtil {
  public static void main(String args[]) throws Exception {
    ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class);

    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




Previous                                                 Next                                                 Home

No comments:

Post a Comment