Spring allows us to create beans that are wired with Collections and Arrays. In this post we shall create a simple class that has different collection based properties and then wire them with simple string values.
Consider the simple Anchor class. Like our other classes it also extends the IPerformer Interface.
For the map the <map> element is used. Each entry in the map is represented by an <entry> element. The entry element has a key attribute and a value attribute to hold the key-value pairs.
The last of the lot is the properties. Every property is represented by a <props> element. The key attribute holds the name of the property while the actual value is simply a text data.
Although the same list element is used to configure the different collections, it does not impact the actual behaviour of the Collection type. For example,consider the below configuration.
If we were to create our anchor bean and call the perform method:
Consider the simple Anchor class. Like our other classes it also extends the IPerformer Interface.
public class Anchor implements IPerformer { private Collection<String> credits; private List<String> notes = new LinkedList<String>(); private Set<String> accolades = new HashSet<String>(); private String[] supportingCastNames; private Map<String, String> winners = new HashMap<String, String>(); private Properties dressDetails; @Override public void perform() { System.out.println("Dress Details : "); Properties properties = this.dressDetails; for (Object key : properties.keySet()) { System.out.println(key + " - " + properties.getProperty(key.toString())); } System.out.println("Notes : "); int cnt = 0; for (String note : notes) { System.out.println((++cnt) + " : " + note); } System.out.println("The winners are as follows"); Set<String> winnerKey = this.winners.keySet(); for (String key : winnerKey) { System.out.println("The winner for " + key + " is " + this.winners.get(key)); } System.out.println("The accolades to shower"); for (String accolade : accolades) { System.out.println(accolade); } System.out.println("Special thanks to the supporting cast : "); cnt = 0; for (String castName : supportingCastNames) { System.out.println((++cnt) + " : " + castName); } System.out.println("..........CREDITS........ "); for (String credit : credits) { System.out.println(credit); } } //setter-getters }The class includes collections, an array and a properties. All hold simple string values. The perform method simply iterates over the members and displays the values. The XML configuration is responsible for wiring the different properties of the bean.
<bean id="anchor" class="com.performer.Anchor"> <property name="credits"> <list> <value>Yank Mildow</value> <value>Ryan Star</value> </list> </property> <property name="notes"> <list> <value>Please take note of the lights above</value> <value>Drinks are served at the counter</value> </list> </property> <property name="accolades"> <list> <value>Ye</value> <value>Rock On</value> </list> </property> <property name="supportingCastNames"> <list> <value>BackStage Music</value> <value>Lights</value> </list> </property> <property name="winners"> <map> <entry key="First Prize" value="Mr Majo" /> <entry key ="Second Prize" value="Miss Mana"></entry> </map> </property> <property name="dressDetails"> <props> <prop key="DressCode">Black</prop> <prop key="Tie">Optional</prop> <prop key="Glasses">Tinted</prop> </props> </property> </bean>The collection, set, list and array property are all configured using the same XML element -<list>. The values are set using the <value> element.
For the map the <map> element is used. Each entry in the map is represented by an <entry> element. The entry element has a key attribute and a value attribute to hold the key-value pairs.
The last of the lot is the properties. Every property is represented by a <props> element. The key attribute holds the name of the property while the actual value is simply a text data.
Although the same list element is used to configure the different collections, it does not impact the actual behaviour of the Collection type. For example,consider the below configuration.
<list> <value>BackStage Music</value> <value>Lights</value> <value>Lights</value> </list>When used to wire in a List Interface the values are perfectly valid as List can have duplicates. The same XML fragment can be associated with a Set, however in that case the duplicate entry would be ignored.
If we were to create our anchor bean and call the perform method:
public void createComplexBeans(final BeanFactory beanFactory) { IPerformer anchor = (IPerformer) beanFactory.getBean("anchor"); System.out.println("Bean received is " + anchor); anchor.perform(); }The output is as below:
Bean received is com.performer.Anchor@1b09468 Dress Details : Glasses - Tinted Tie - Optional DressCode - Black Notes : 1 : Please take note of the lights above 2 : Drinks are served at the counter The winners are as follows The winner for First Prize is Mr Majo The winner for Second Prize is Miss Mana The accolades to shower Ye Rock On Special thanks to the supporting cast : 1 : BackStage Music 2 : Lights ..........CREDITS........ Yank Mildow Ryan StarIn the next post we shall work with collection of Objects instead of values.
No comments:
Post a Comment