Spring Experience 4--setter injection set (set, list, map, properties and many other collections, with case resolution) @ Basic Pack
1. Basic assembly
Piecing a bean inside a spring container is called Assembly . When you assemble a bean, you need to tell the container which beans and how the container uses dependency injection to tie them together.
Using XML assembly (XML is the most common spring application configuration source.) )
Several spring containers support the use of XML assembly beans, including:
1). Xmlbeanfactory: Call InputStream to load the context definition file.
2). Classpathxmlapplicationcontext: Loads the context definition file from the classpath.
3). Xmlwenapplicationcontext: Loads the definition file from the Web application context.
The root element of the context definition file is <beans>.<beans> has multiple <bean> child elements. Each <bean> element defines how a bean is assembled into a spring container. The most basic configuration for a bean consists of the bean's ID and his full name Class name .
Basic Assembly-scope
the Scope property has the following five types of values:prototype, Singleton, request session, Global-session.
beans in spring are singleton mode by default . Always returns an instance. If you want to return different instances, you need to define the prototype pattern.
2. Instantiation and destruction
When spring instantiates a bean or destroys a bean, some processing is sometimes required, so spring can invoke the Bean's two life-cycle methods when creating and dismounting the bean (thebean's declaration cycle is explained in the previous blog with heavy Ink ).
<bean class= "Foo" Init-method destory-method>
<bean class= "... commonannotationbeanpostprocessor" >
Spring also provides two interfaces to achieve the same functionality:
The Initializingbean and Disposablebean.initializingbean interfaces provide a Afterpropertiesset () method. The Disposablebean interface provides destroy (). This interface is not recommended, and it will set your bean and springapi state together.
3. Some considerations
Inherit the configuration (inherited in the bean label plus attribute of the Parent property to indicate that the property value is inherited from the parent bean's ID), overriding the parent bean configuration.
You can set the abstract property of <bean> to True, Spring does not instantiate the bean, and some properties are not inherited. For example: Autowire, abstract and so on. The child bean specifies its own class. However, abstract must be set to true at this time
Dependency Injection via set method
The < property > child elements of the <bean> element indicate the use of their set method to inject. Can inject anything from the basic type to the collection class, or even the bean of the application system
Configures the bean's simple properties, base data type, and string.
Setting a bean type attribute in the property sub-label of the corresponding bean instance, the disadvantage of which is that you cannot reuse the bar instance elsewhere because it is used specifically for Foo.
4.setter Injection Set
assemble list and array:
<property name= "Barlist" >
<list>
<value>bar1</value>
<ref bean= "Bar2"/>
</list>
</property>
Assembly set:
<property name= "Barlist" >
<set>
<value>bar1</value>
<ref bean= "Bar2"/>
</set>
</property>
The set uses the same method as the list, where the object is assembled into the set, and the list is assembled into a list or array
Assembly Map:
<property name= "Barlist" >
<map>
<entry key= "Key1" value= "Bar1"/>
<entry key= "Key2 value-ref=" xxx "/>
</map>
</property>
The key value must be string, and key-ref can be a different bean.
Set NULL:
<property name= "Barlist" >
<null/>
</property>
Case studies of injected collections
the property naming and access modifiers in the following classes are all for testing purposes, such as the following properties are public types. The actual development is private type, access the property through the Get method, this is only for simple testing.
Collection Bean Collectionbean Class
Package www.csdn.spring.collection.set; Import Java.util.list;import java.util.map;import java.util.properties;import java.util.Set; Publicclass Collectionbean {//set collection public set<string> sets; Publicvoid setsets (set<string> sets) {this.sets = sets; } public Collectionbean () {System.out.println ("Initialize ..... "); }//list Collection public list<user> users; Publicvoid setusers (list<user> users) {this.users = users; }//map set public map<integer,user> Map; Publicvoid Setmap (Map<integer, user> map) {this.map = map; }//properties Collection public Properties props; Publicvoid SetProps (Properties props) {this.props = props; }} helper class Userpackage Www.csdn.spring.collection.set; Publicclass User {public String name; Public Integer age; Publicvoid setName (String name) {this.name = name; } publicvoid Setage (Integer age) {this.age = age; }} Test Class Testbeanpackage Www.csdn.spring.collection.set; Import Java.util.iterator;import java.util.list;import Java.util.map;import Java.util.map.entry;import Java.util.properties;import Java.util.Set; Import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.ClassPathXmlApplicationContext; Publicclass Testbean {@Test publicvoid Test () {ApplicationContext context = new Classpathxmlapplicationcontext ("Spring-collection.xml"); Collectionbean bean = Context.getbean ("Collectionbean", Collectionbean.class); Set Set set<string> sets = Bean.sets; Get the iterator iterator<string> it = Sets.iterator (); while (It.hasnext ()) {System.out.println (It.next ()); } System.out.println ("-------------------------list collection------------------------"); List Collection List<user> users = bean.users; for (User user:users) {System.out.println (user.name+ "---" +user.age); } System.out.println ("--------------------------Map Collection------------------------"); Map Collection//Method one: map<integer,user> map = Bean.map; Get the set set of the key key of map set<integer> SetKeys = Map.keyset (); The iterator that gets the key key iterator<integer> Itkeys = Setkeys.iterator (); Iteration Key value while (Itkeys.hasnext ()) {//Get a specific key value Integer key = Itkeys.next (); The value that corresponds to the key is obtained by the Get (key) method. User user = Map.get (key); System.out.println (key+ "--" +user.name+ "=" +user.age "); } System.out.println ("========================"); Method Two: Set<entry<integer,user>> setentry = Map.entryset (); iterator<entry<integer,user>> itentry = Setentry.iterator (); while (Itentry.hasnext ()) {entry<integer,user> Entry = Itentry.next (); User user = Entry.getvalue (); System.out.println (Entry.getkey () + "---" +user.name+ "=" +user.age); } System.out.println ("-------------------------Properties collection------------------------"); Properties Collection Properties props = Bean.props; set<string> SetProps = Props.stringpropertynames (); iterator<string> keystr = Setprops.iterator (); while (Keystr.hasnext ()) {String key = Keystr.next (); Use the GetProperty (key) method to obtain the value of the key corresponding to System.out.println (key+ "----" +props.getproperty (key)); }}} Spring configuration file <?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "http://www.springframework.org/schema/ Beans "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://www.springframework.org/ Schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id=" Collectionbean "C lass= "Www.csdn.spring.collection.set.CollectionBean" > <!--Set Set--<property name= "Sets" > <set> <value>Qnovations </value> <value> yangkai </value> <value> li </value> <VALUE&G t; Chubby </value> <value> chic </value> </set> </property> <!--list Collection--<property name= "Users" > <array> <ref bean= "U1"/> <ref b Ean= "U2"/> <ref bean= "U3"/> </array> <!--<list> <ref bean= "U1"/&G T <ref bean= "U2"/> <ref bean= "U3"/> </list>-</property> <!--map Collection-- <property name= "Map" > <map> <entry key= "1" value-ref= "U1"/> <entry key = "2" > <ref bean= "U2"/> </entry> <entry key= "3" value-ref= "U3"/> ; </map> </property> <!--Properties Collection--<property name= "props" > <props& Gt <prop key= "1" >jdbc:oracle</prop> <prop key= "2" >jdbc:mysql</prop> <pro P key= "3" >jdbc:access</prop> </props> </property> </bean> <bean id= "U1" C lass= "Www.csdn.spring.collection.set.User" > <property name= "name" value= "Yangkai"/> <property name= "age" Value= "/> </bean> <bean id=" U2 "class=" Www.csdn.spring.collection.set.User "> <property nam E= "name" value= "Chic"/> <property name= "age" value= "/>" </bean> <bean id= "U3" class= "www.csdn . spring.collection.set.User "> <property name=" name "value=" Red Army "/> <property name=" age "value=" 28 "/& Gt </bean> </beans>
Spring 4--setter Injection Set (set, list, map, properties, and many more collections, with case resolution) @ Base mount (Reference)