In my
previous posts, I explained how Owner API maps a property to Custom object,
primitive types. In addition to these, Owner API also provides support for
Arrays and Collections.
import java.util.List; import org.aeonbits.owner.Config; public interface ProjectConfig extends Config { @DefaultValue("Saturday, Sunday") String[] days(); @DefaultValue("January, February, March, April") String[] month(); @DefaultValue("1988, 1989") List<Integer> year(); }
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<>(); properties1.put("days", "Tuesday, Wednesday, Thrusday, Friday"); properties1.put("month", "October, November, December"); ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class, properties1); String[] days = cfg.days(); String[] months = cfg.month(); List<Integer> years = cfg.year(); System.out.println("----------------------"); for (String day : days) { System.out.println(day); } System.out.println("----------------------"); for (String month : months) { System.out.println(month); } System.out.println("----------------------"); for (int year : years) { System.out.println(year); } } }
Output
---------------------- Tuesday Wednesday Thrusday Friday ---------------------- October November December ---------------------- 1988 1989
@DefaultValue("January, February, March,
April")
As you see,
by default elements in array (or) collection are separated by ,.
You can
change this default separator to any string.
For example,
@Separator(";")
@DefaultValue("January;
February; March; April")
String[]
month();
Above
snippet uses ; as separator value.
import java.util.List; import org.aeonbits.owner.Config; public interface ProjectConfig extends Config { @Separator("_") @DefaultValue("Saturday$Sunday") String[] days(); @Separator(";") @DefaultValue("January; February; March; April") String[] month(); @Separator("-") @DefaultValue("1988-1989") List<Integer> year(); }
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<>(); properties1.put("days", "Tuesday_ Wednesday_ Thrusday_ Friday"); properties1.put("month", "October; November; December"); ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class, properties1); String[] days = cfg.days(); String[] months = cfg.month(); List<Integer> years = cfg.year(); System.out.println("----------------------"); for (String day : days) { System.out.println(day); } System.out.println("----------------------"); for (String month : months) { System.out.println(month); } System.out.println("----------------------"); for (int year : years) { System.out.println(year); } } }
Output
---------------------- Tuesday Wednesday Thrusday Friday ---------------------- October November December ---------------------- 1988 1989
Just like
how you specify @Separator annotation on method level, you can also specify it
on class level.
import java.util.List; import org.aeonbits.owner.Config; import org.aeonbits.owner.Config.Separator; @Separator(";") public interface ProjectConfig extends Config { @DefaultValue("Saturday;Sunday") String[] days(); @DefaultValue("January; February; March; April") String[] month(); @Separator("_") @DefaultValue("1988_1989") List<Integer> year(); }
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<>(); properties1.put("days", "Tuesday; Wednesday; Thrusday; Friday"); properties1.put("month", "October; November; December"); ProjectConfig cfg = ConfigFactory.create(ProjectConfig.class, properties1); String[] days = cfg.days(); String[] months = cfg.month(); List<Integer> years = cfg.year(); System.out.println("----------------------"); for (String day : days) { System.out.println(day); } System.out.println("----------------------"); for (String month : months) { System.out.println(month); } System.out.println("----------------------"); for (int year : years) { System.out.println(year); } } }
Output
---------------------- Tuesday Wednesday Thrusday Friday ---------------------- October November December ---------------------- 1988 1989
Note
The Map
interface and sub-interfaces are not supported.
No comments:
Post a Comment