Know Spring-1.11 from scratch inject list or set (this example shows code reuse)

Source: Internet
Author: User

Let's talk about injecting a list or set in this section.

The example in our chapter is that different cooks use different ovens to make different cakes.

1.domain

Cake Category: (following the cake in the previous section)

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_11;public class Cake {private final int id = index++; private static int index = 0;private String name = "";p rivate Double size = 0;public string GetName () {return name;} public void SetName (String name) {this.name = name;} Public double GetSize () {return size;} public void SetSize (double size) {this.size = size;} public int getId () {return ID;} @Overridepublic String toString () {return "Create the Cake,its ID:" + ID + ", size:" + size + "inch, Name:" + Name;}}

Chef class:

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_11;import Java.util.arraylist;import Java.util.hashset;import Java.util.iterator;public class Chief {private static int index = 0;private arraylist<cake& Gt Cakes = null;private final int id = index++;p rivate String name = "";p rivate hashset<oven> ovens = Null;public Array List<cake> Getcakes () {return cakes;} public int getId () {return ID;} Public String GetName () {return name;} Public hashset<oven> Getovens () {return ovens;} Public arraylist<cake> Makecakes () {for (iterator<cake> Iterator = Cakes.iterator (); Iterator.hasnext ();) {Cake Cake = Iterator.next (); SYSTEM.OUT.PRINTLN (name + cake);} return Getcakes ();} public void Userovens () {for (iterator<oven> Iterator = Ovens.iterator (); Iterator.hasnext ();) {Oven Oven = Iterator.next (); SYSTEM.OUT.PRINTLN ("use" + oven);}} public void Setcakes (arraylist<cake> cakes) {this.cakes = cakes;} public void SetName (String name) {this.name = name;} Public void Setovens (hashset<oven> ovens) {this.ovens = ovens;}} 

Chef class here to explain:

(1) In order to use the different ovens, we joined the ovens, because the actual situation only the size of the oven each one, so we just use set, instead of using the list

(2) In order to be able to make different cakes, we use a list to place different cakes, and here is allowed to repeat, so the list is used here

(3) For ease of output, I output data directly in Userovens and Makecakes


Oven class: (no change, just remove the ID, add the name attribute, so that the output is convenient)

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_11;public class Oven {private String name = ""; Overridepublic String toString () {return name;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}

2. Test class:

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_11;import Org.junit.test;import Org.junit.runner.runwith;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.applicationcontext;import org.springframework.test.context.ContextConfiguration; Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith (Springjunit4classrunner.class) @ Contextconfiguration (locations = {"/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_11/ Applicationcontext-test.xml "}) public class Chieftest {@Autowiredprivate ApplicationContext applicationcontext;@ testpublic void Testchief () {Chief Jack = (Chief) Applicationcontext.getbean ("Jack"); Jack.userovens (); Jack.makecakes ( ); Chief Rose = (chief) Applicationcontext.getbean ("Rose"); Rose.userovens (); Rose.makecakes ();}}

The test class continues in the previous way, removing different chef objects from the container, and the chef starts using the oven to make the cake.


3. Configuration files (emphasis is also where code reuse is reflected)

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:p= "http://www.springframework.org/schema/p" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= " Http://www.springframework.org/schema/aop "xsi:schemalocation=" Http://www.springframework.org/schema/beans http ://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http ://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/tx http:/ /www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/util/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/UTIL/SPRING-UTIL-3.1.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/DATA/JPA/HTTP Www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsdhttp://www.springframework.org/schema/beans/HTTP Www.sprinGframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop.xsd "><bean id=" Blueberrycheesecake "class=" com.raylee.my_new _spring.my_new_spring.ch01.topic_1_11.cake "p:name=" Blueberry Cheese Cake "p:size=" 5 "scope=" prototype "></ Bean><bean id= "Chocolatecheesecake" class= "Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_11.Cake" P: Name= "Chocolate Cheese Cake" p:size= "6" scope= "prototype" ></bean><bean id= "Bananaaatmelmoussecake" class = "Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_11.Cake" p:name= "banana Oatmel mousse Cake" p:size= "7" scope= " Prototype "></bean><bean id=" Vanillaeclair "class=" com.raylee.my_new_spring.my_new_spring.ch01.topic_ 1_11.cake "p:name=" Vanilla eclair "p:size=" 8 "scope=" prototype "></bean><bean id=" Ligueurperfumedtripletcake "class=" Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_11.Cake "P:name=" ligueur Perfumed triplet cake "p:Size= "5.5" scope= "prototype" ></bean><bean id= "Bigoven" class= "Com.raylee.my_new_spring.my_new_ Spring.ch01.topic_1_11.Oven "P:name=" Bigoven "/><bean id=" Smalloven "class=" Com.raylee.my_new_spring.my_new_ Spring.ch01.topic_1_11.Oven "P:name=" Smalloven "/><bean id=" Jack "class=" Com.raylee.my_new_spring.my_new_ Spring.ch01.topic_1_11.Chief "P:name=" Jack "><property name=" Ovens "><set><ref bean=" Bigoven "/ ><ref bean= "Bigoven"/><ref bean= "Smalloven"/></set></property><property name= "cakes "><list><ref bean=" Blueberrycheesecake "/><ref bean=" Chocolatecheesecake "/><ref bean=" Bananaaatmelmoussecake "/><ref bean=" Vanillaeclair "/></list></property></bean>< Bean id= "Rose" class= "Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_11.Chief" P:name= "Rose" ><property Name= "Ovens" ><set><ref bean= "Smalloven"/></set></property><property name= "cakes" >&Lt;list><ref bean= "Vanillaeclair"/><ref bean= "Ligueurperfumedtripletcake"/><ref bean= " Chocolatecheesecake "/></list></property></bean></beans>

A place to explain:

(1) We use a cake class to create different categories of cake objects by changing the name and other attributes.

(2) We use an oven class to create different types of oven objects by changing the name and other properties

(3) We use a chef class to create different categories of Chef objects by changing the name and other attributes.

Note: The above comparison can reflect the code reuse

(4) Each cake bean needs to use the scope of the prototype, so that each time the creation of a different cake object, or the next get cake will appear two of the same ID cake, this is obviously not in line with the actual situation.

(5) But the oven is not the same, because for the bakery, the number of ovens is certain, can not appear multiple ovens, so they must use the default singleton mode

(6) When you configure the oven properties in the chef, we use set, so that even if the configuration is more than the above, it will not be repeated, because this collection has the characteristics of the set, while configuring the cake properties We use list, because each cake is different, So using list is more appropriate

(7) List and set in addition to the above can be put into the bean, you can also put value, this time only need to use the <value/> tag, not the use of <ref/>, but because the injection value is relatively small, so no detailed explanation.


Test output:

Use Bigoven
Use Smalloven
Jack Create the Cake,its id:0, size:5.0 inch, Name:blueberry cheese cake
Jack Create the Cake,its id:1, size:6.0 inch, name:chocolate cheese cake
Jack Create the Cake,its Id:2, size:7.0 inch, Name:banana Oatmel mousse Cake
Jack Create the Cake,its Id:3, size:8.0 inch, Name:vanilla Eclair
Use Smalloven
Rose Create the Cake,its id:4, size:8.0 inch, Name:vanilla Eclair
Rose Create the Cake,its id:5, size:5.5 inch, name:ligueur perfumed triplet cake
Rose Create the Cake,its id:6, size:6.0 inch, name:chocolate cheese cake


Summary: In this chapter we mainly introduce the injection of list or set.


Catalog: http://blog.csdn.net/raylee2007/article/details/50611627

My github:https://github.com/raylee2015/my_new_spring.




Inject list or set from Spring-1.11 (This example shows code reuse)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.