|
|||||
|
|||||
Concise Collection Literal
PropertyEditorNuts allows custom PropertyEditor classes to be used to convert string literal to any target type. For example, you could create a BankAccountEditor class for BankAccount. So that the following configuration creates a BankAccount object: <value id="account" type="BankAccount"> id=abc balance=100 </value> Concise Collection LiteralWithout relying on PropertyEditor, Nuts supports concise collection literal so that one can configure collection object using a simple xml attribute value. Array, java.util.List, java.util.Map, java.util.Set, java.util.Properties etc. are used very frequently in many programs. It is pretty inconvenient if we have to use a <ctor> tag and a series of <method> tag to construct such object in order to wire it to a component. Nuts supports concise collection literal that allows you to create an array/list/map with a simple syntax. To create an array or list or set, simply seperate items with a comma. For example: <value id="myarray" val="a,b,100"/> This value represents an array/list/set of 3 elements: a, b and 100. To create a map, simple enclose it with a pair of { and }: <value id="mymap" val="{name=Ben,age=10}"/> This creates a map with two entries. Don't worry about the type, Nuts will try best to automatically convert it to the expected type, be it java.util.Properties, java.util.LinkedHashMap, java.util.TreeSet or java.util.LinkedList. It is also allowed to use variable names inside these literals. For example, we could parameterize the key "name" and the value "10" with two variables $namekey and $ageval: <value id="mymap" val="{$namekey=Ben,age=$ageval}"/> Or, to parameterize the list elements: <value id="mymap" val="$first, $second"/> Collection literal is handy in simplying the syntax of <ctor>, <method>, <bean> tags because we don't have to always use the heavy xml tags to specify method/constructor arguments and bean properties. Created by benyu |
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||