Learn from the beginning Spring-1.4 inject beans through a constructor

Source: Internet
Author: User

In this chapter we introduce the injection of beans through a constructor.

1. How does life a bean?

(1) Create a class

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_2;public class Song {private String Name;public song ( String name) {this.name = name;} Public Song () {} @Overridepublic String toString () {return "the Song:" + Name;}}

(2) Configuring XML

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" 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= "song" class= "Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_2.Song" ><constructor-arg value= "there would be "/></bean></beans>



2. How do I inject beans through a constructor?

Here's an example of a chef making a cake.

(1) Create a chef class

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4;public class Chief {private Cake Cake = Null;public Chief (Cake Cake) {//Where the constructor is injected this.cake = cake;} public void Makeonecake () {System.out.println (cake.tostring ());}}

When we build the chef's objects, we pass on the objects that we need to make.


(2) Create cake class

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4;public class Cake {private final int id = index++;p rivate static int index = 0; @Overridepublic String toString () {return ' Create the Cake,its ID: ' + ID;}}

Identify the ID of each object by final

And then just print a sentence that has been created


(3) configuration file

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" 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= "Cake" class= "com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4.Cake"/><bean id= "chief" class= " Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4.Chief "><constructor-arg ref=" Cake "/></bean ></beans>

Configuration files are mainly configured here through bean tags.

In the bean tag can also write Constructor-arg, here is through the constructor injection, he has two parameters, one is ref, reference one of the above bean, one is value, write the value directly


(4) Create a test class

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4;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 = {//"Classpath:applicationcontext-test.xml"}) @ContextConfiguration (locations = { "/com/raylee/my_new_spring/my_new_spring/ch01/topic_1_4/applicationcontext-test.xml"}) public class ChiefTest {@ Autowiredprivate applicationcontext applicationcontext; @Testpublic void Testchief () {Chief Chief = Applicationcontext.getbean (Chief.class); Chief.makeonecake ();}}

If you are building applicationcontext alone, you can use the way I commented above, because we are here to put together multiple projects, so I'm referencing an absolute path.


Output:

Create the Cake,its id:0


3. If the class does not have a public constructor, what if a singleton class is used?

According to the code above, we add an oven, while our oven chooses a singleton mode.

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4;public class Oven {private Oven () {}public static Oven GetInstance () {return new Oven ();} @Overridepublic String toString () {return ' use old oven ';}}


The chef then adds the oven to the tool:

Package Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4;public class Chief {private Cake Cake = Null;private Oven Oven = Null;public Chief (Cake Cake) {this.cake = Cake;} Public chief (Cake Cake, Oven Oven) {this.cake = Cake;this.oven = Oven;} public void Makeonecake () {System.out.println (oven.tostring () + cake.tostring ());}}


When configured, a factory method is specially configured for the oven, and the Factory-method is set for a single case:

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: context= "Http://www.springframework.org/schema/context" 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= "Cake" class= "com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4.Cake"/><bean id= "chief" class= " Com.raylee.my_new_spring.my_new_spring.ch01.topic_1_4.Chief "><constructor-arg ref=" Cake "/>< Constructor-arg ref= "oven"/></bean><bean id= "oven" class= "com.raylee.my_new_spring.my_new_ Spring.ch01.topic_1_4.Oven "factory-method=" getinstance "/></beans>


Test output:

Use old Ovencreate the Cake,its id:0

Summary: This section describes how to inject beans through a constructor.


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




Learn from the beginning Spring-1.4 inject beans through a constructor

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.