Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Sping Slide 6
1. Collections for Bean Properties Java Collections Setting Collection for Bean Properties Defining List,Set,Map,Properties Setting Data Type for Collections Defining Concrete Collection Classes using Factory Bean Defining Concrete Collection Classes using Schema Defining Stand Alone Collections
2. The Java Collections framework defines a set of interfaces, implementations, and algorithms for different types of collections, such as lists, sets, and maps List, Set, and Map are the core interfaces representing three main types of collections. For each collection type, Java provides several implementations with different functions and characteristics from which you can choose. In Spring, these types of collections can be easily configured with a group of built-in XML tags, such as <list>, <set>, and <map>
3.
4.
5. First, let’s use a java.util.List collection to contain your suffixes. A list is an ordered and indexed collection whose elements can be accessed either by index or with a for-each loop. public class SequenceGenerator { ... private List<Object> suffixes; public void setSuffixes(List<Object> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... for (Object suffix : suffixes) { buffer.append("-"); buffer.append(suffix); } return buffer.toString(); } } Using List example Bean
6. To define a property of java.util.List type in the bean configuration, you specify a <list> tag that contains the elements. The elements allowed inside the <list> tag can be a simple constant value specified by <value>, a bean reference by <ref>, an inner bean definition by <bean>, or a null element by <null>. You can even embed other collections in a collection. <bean id="sequenceGenerator" class="SequenceGenerator"> <property name="prefixGenerator" ref="datePrefixGenerator" /> <property name="initial" value="100000" /> <property name="suffixes"> <list> <value>A</value> <bean class="java.net.URL"> <constructor-arg value="http" /> <constructor-arg value="www.javafasttrack.com" /> <constructor-arg value="/" /> </bean> <null /> </list> </property> </bean> Using List example Configuration
7. public class SequenceGenerator { ... private Set<Object> suffixes; public void setSuffixes( Set<Object> suffixes) { this.suffixes = suffixes; } ... } To define a property of java.util.Set type, use the <set> tag to define the elements in the same way as a list. <bean id="sequenceGenerator" class="SequenceGenerator"> ... <property name="suffixes"> <set> <value>A</value> <bean class="java.net.URL"> <constructor-arg value="http" /> <constructor-arg value="www.javafasttrack.com" /> <constructor-arg value="/" /> </bean> <null /> </set> </property> </bean> Using Set example Bean Using Set example Configuration
8. Using Maps example Bean public class SequenceGenerator { ... private Map<Object, Object> suffixes; public void setSuffixes(Map<Object, Object> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... for (Map.Entry entry : suffixes.entrySet()) { buffer.append("-"); buffer.append(entry.getKey()); buffer.append("@"); buffer.append(entry.getValue()); } return buffer.toString(); } }
10. There are shortcuts to defining map keys and values as attributes of the <entry> tag. If they are simple constant values, you can define them by key and value. If they are bean references, you can define them by key-ref and value-ref. <bean id="sequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator"> ... <property name="suffixes"> <map> <entry key="type" value="A" /> <entry key="url"> <bean class="java.net.URL"> <constructor-arg value="http" /> <constructor-arg value="www.apress.com" /> <constructor-arg value="/" /> </bean> </entry> </map> </property> </bean>
11. Using java.util.Properties A java.util.Properties collection is very similar to a map. It also implements the java.util.Map interface and stores entries in key/value pairs. The only difference is that the keys and values of a Properties collection are always strings....
12. public class SequenceGenerator { ... private Properties suffixes; java.util.Properties Bean example public void setSuffixes(Properties suffixes) { this.suffixes = suffixes; } ... } To define a java.util.Properties collection in Spring, use the <props> tag with multiple <prop> tags as children. Each <prop> tag must have a key attribute defined and the corresponding value enclosed. <bean id="sequenceGenerator“ class="SequenceGenerator"> ... <property name="suffixes"> <props> <prop key="type">A</prop> <prop key="url">http://www.jftcom/</prop> </props></property> </bean>
13. Merging the Collection of the Parent Bean If you define your beans with inheritance, a child bean’s collection can be merged with that of its parent by setting the merge attribute to true. For a <list> collection, the child elements will be appended after the parent’s to preserve the order. So, the following sequence generator will have four suffixes: A, B, A, and C.
15. Merging the Collection of the Parent Bean Example (Contd ..) </bean> ... </beans> For a <set> or <map> collection, the child elements will overwrite the parent’s if they have the same value. So, the following sequence generator will have three suffixes: A, B, and C. <beans ...> <bean id="baseSequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator"> <property name="prefixGenerator" ref="datePrefixGenerator" /> <property name="initial" value="100000" /> <property name="suffixes"> <set> <value>A</value> <value>B</value> </set> </property> </bean>
18. Now suppose you are going to accept a list of integer numbers as the suffixes of your sequence generator. Each number will be formatted into four digits by an instance of java.text. DecimalFormat. public class SequenceGenerator { ... private List<Object> suffixes; public void setSuffixes(List<Object> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... DecimalFormat formatter = new DecimalFormat("0000"); for (Object suffix : suffixes) { buffer.append("-"); buffer.append(formatter.format((Integer) suffix)); } return buffer.toString(); } }
19. <bean id="sequenceGenerator" class="com.apress.springrecipes.sequence.SequenceGenerator"> <property name="prefixGenerator" ref="datePrefixGenerator" /> <property name="initial" value="100000" /> <property name="suffixes"> <list> <value>5</value>However, when you run this application, you will encounter a ClassCastException, indicating that the suffixes cannot be cast into integers because their type is String. Spring treats every element in a collection as a string by default <value>10</value> <value>20</value> </list> </property> </bean>
20. <bean id="sequenceGenerator" class="SequenceGenerator"> ... <property name="suffixes"> <list> <value type="int">5</value> <value type="int">10</value> <value type="int">20</value> </list> </property> </bean> Or you may set the value-type attribute of the collection tag to specify the type for all elements in this collection. <bean id="sequenceGenerator" class="SequenceGenerator"> ... <property name="suffixes"> <list value-type="int"> <value>5</value> <value>10</value> <value>20</value> </list> </property> </bean>
21. In Java 1.5 or higher, you can define your suffixes list with a type-safe collection that stores integers. ... public class SequenceGenerator { ... private List<Integer> suffixes; public void setSuffixes(List<Integer> suffixes) { this.suffixes = suffixes; } public synchronized String getSequence() { StringBuffer buffer = new StringBuffer(); ... DecimalFormat formatter = new DecimalFormat("0000"); for (int suffix : suffixes) { buffer.append("-"); buffer.append(formatter.format(suffix)); }
22. return buffer.toString(); } } Once you have defined your collections in a type-safe way, Spring will be able to read the collection’s type information through reflection. In this way, you no longer need to specify the value-type attribute of <list>. <bean id="sequenceGenerator“ class="SequenceGenerator"> ... <property name="suffixes"> <list> <value>5</value> <value>10</value> <value>20</value> </list> </property> </bean>
23. class CarFactory{ Car static newInstance(){ return new Maruthi(); } } interface Car{ } Maruthi implements Car{ }
26. Or you can use a collection tag in the util schema to define a collection and set its target class (e.g., by the set-class attribute of <util:set>). But you must remember to add the util schema definition to your <beans> root element. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd"> <bean id="sequenceGenerator“ class=“SequenceGenerator"> ... <property name="suffixes"> <util:set set-class="java.util.TreeSet"> <value>5</value> <value>10</value> <value>20</value> </util:set> </property> </bean> ... </beans>
29. Or you can define a stand-alone set by using the <util:set> tag in the util schema. <beans ...> <bean id="sequenceGenerator“ class="SequenceGenerator"> ... <property name="suffixes"> <ref local="suffixes" /> </property> </bean> <util:set id="suffixes"> <value>5</value> <value>10</value> <value>20</value> </util:set> ... </beans>
30.
31.
32.
33.
34.
35.
36.
37.
38. class CarFactory{ Car static newInstance(){ return new Maruthi(); } } interface Car{ } Maruthi implements Car{ }