{title:'Sections', updated:'9.0.0'}

Config sections can be retrieved in-bulk using the {@link oaj.config.Config#getSection(String)} method. It returns the following bean:

- {@link oaj.config.Section}
  - {@link oaj.config.Section#asBean(Class) asBean(Class)}
  - {@link oaj.config.Section#asBean(Class,boolean) asBean(Class,boolean)}
  - {@link oaj.config.Section#asInterface(Class) asInterface(Class)}
  - {@link oaj.config.Section#asMap() asMap()}
  - {@link oaj.config.Section#isPresent() isPresent()}
  - {@link oaj.config.Section#writeToBean(Object,boolean) writeToBean(Object,boolean)}

The {@link oaj.config.Section#asMap() asMap()} method allows you to access a section as simple key/value pairs.

##### Example:

| // Example config file | [MyAddress] | street = 123 Main Street | city = Anywhere | state = NY | zip = 12345

| // Example usage | Config config = Config.create("MyConfig.cfg").build(); | | JsonMap map = config.getSection("MyAddress").asMap().get(); | | String street = map.getString("street"); | String city = map.getString("city"); | String state = map.getString("state"); | int zip = map.getInt("zip");

Maps created this way are snapshot copies of the section at the time of the method call.

Config files can also be used to directly populate beans using {@link oaj.config.Section#asBean(Class) asBean()} or {@link oaj.config.Section#writeToBean(Object,boolean) writeToBean()}.

##### Example:

| // Example config file | [MyAddress] | street = 123 Main Street | city = Anywhere | state = NY | zip = 12345

| // Example bean | public class Address { | public String street, city; | public StateEnum state; | public int zip; | } | | // Example usage | Config config = Config.create("MyConfig.cfg").build(); | Address myAddress = config.getSection("MyAddress").as(Address.class).orElse(null);

Like maps, beans created this way are snapshot copies of the section at the time of the method call.

Config sections can also be accessed via interface proxies using {@link oaj.config.Section#asInterface(Class)}.

While section maps and beans retrieve copies of the configuration data at the time of the method call, section interfaces can also be use to set values in the underlying configuration.

##### Example:

| // Example config file | [MySection] | string = foo | int = 123 | enum = ONE | bean = {foo:'bar',baz:123} | int3dArray = [[[123,null],null],null] | bean1d3dListMap = {key:[[[[{foo:'bar',baz:123}]]]]}

| // Example interface. | // Setters are optional. | public interface MyConfigInterface { | | String getString(); | void setString(String value); | | int getInt(); | void setInt(int value); | | MyEnum getEnum(); | void setEnum(MyEnum value); | | MyBean getBean(); | void setBean(MyBean value); | | int[][][] getInt3dArray(); | void setInt3dArray(int[][][] value); | | Map<String,List<MyBean[][][]>> getBean1d3dListMap(); | void setBean1d3dListMap(Map<String,List<MyBean[][][]>> value); | } | | // Example usage. | Config config = Config.create("MyConfig.cfg").build(); | MyConfigInterface intf = config.getSection("MySection").asInterface(MyConfigInterface.class).get(); | | // Read a value. | int myInt = intf.getInt(); | | // Write a value. | intf.setBean(new MyBean()); | | // Commit your changes to the store. | config.commit();