blob: 58fd2c956f67b7a763bb1112932c4b87d0d78188 [file] [log] [blame]
<!--
/***************************************************************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
***************************************************************************************************************************/
-->
Section Beans
<p>
Config files can also be used to directly populate beans using
{@link oaj.config.Config#getSectionAsBean(String,Class,boolean)}.
</p>
<h5 class='figure'>Example:</h5>
<p class='bpcode w800'>
<jc>// Example config file</jc>
<cs>[MyAddress]</cs>
<ck>street</ck> = <cv>123 Main Street</cv>
<ck>city</ck> = <cv>Anywhere</cv>
<ck>state</ck> = <cv>NY</cv>
<ck>zip</ck> = <cv>12345</cv>
</p>
<p class='bpcode w800'>
<jc>// Example bean</jc>
<jk>public class</jk> Address {
<jk>public</jk> String <jf>street</jf>, <jf>city</jf>;
<jk>public</jk> StateEnum <jf>state</jf>;
<jk>public int</jk> <jf>zip</jf>;
}
<jc>// Example usage</jc>
Config c = Config.<jsm>create</jsm>(<js>"MyConfig.cfg"</js>).build();
Address myAddress = c.getSectionAsBean(<js>"MyAddress"</js>, Address.<jk>class</jk>);
</p>
<p>
Like maps, beans created this way are snapshot copies of the section at the time of the method call.
</p>