In previous post, I explained about the
load startegy First, In this post, I am going to explain about load strategy
Merge.
Previous
Next
Home
@LoadPolicy(LoadType.MERGE)
@Sources({
"file:~/.myapp.config",
"classpath:App.properties",
"classpath:Con.properties" })
In above snippet, owner API first tries
to find tge property int the file "file:~/.myapp.config", if the
property exists, then it return the value associated with this property. If the
property not exist in first file, it look into the second file
"classpath:App.properties", if the property exists, it return the
value associated with it, else it look into the third file
"classpath:Con.properties", if the property exists, then it return
the value associated with it, else it return the value specified by the
@DefaultValue if specified, otherwise null will be returned.
App.properties
#Project configurations videos.directory = /Users/harikrishna_gurram/videos maxIcons = 9
Con.properties
#Project configurations images.directory = /Users/harikrishna_gurram/images images.backup.directory = /Users/harikrishna_gurram/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(); }
PropertyUtil.java
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
Note
~ points to
the Home directory in your machine.
$ echo ~
/Users/harikrishna_gurram
No comments:
Post a Comment