Spring inversion of control

Source: Internet
Author: User
Tags getcolor

IOC (inversion of control) controlled inversion

My understanding: The control of creating objects is transferred from the code itself to the external container (Spring container).

1. transfer control of the Component object (business object) from the code itself to the external container.

Instead of using the keyword new in your code to build a business instance, it is in the configuration file. The XML node knows how the container names the object name of the corresponding type built in memory .

DI Dependency Injection ( Dependency Injection )Di Dependency Injection: When an object instance is created, an attribute value or other object instance (domain attribute) is injected for the object.

<!--ioc-->

<Bean id= "Happyservice" class= "Cn.happy.Service.HappyService " >

<!--DI Property value Dependency Injection--

< Property name= ' info ' value= ' Spring '></property >

Here is a case of a cartridge

Ink Color paper paper print printer

/**
* Created by Administrator on 2017/7/23. The interface of the cartridge
*/
Public Interface Ink {

Ways to get colors

Public String GetColor ();


/**
* Created by Administrator on 2017/7/23. Implementation class

What color of the ink cartridges
*/
Public class Grayink implements ink{
Public String GetColor () {
return "Black and white cartridge";
}
}

/**
* Created by Administrator on 2017/7/23. Paper interface
*/
Public Interface Paper {
Public String Getpaper ();
}


/**
* Created by Administrator on 2017/7/23. Implement class B5 type of paper
*/
Public class B5paper implements paper{
Public String Getpaper () {
return "B5 paper";
}
}

**
* Created by Administrator on 2017/7/23. Printer
*/
Public class Printer {

Domain Properties:
PrivateInkInk;
PrivatePaperPaper;
Public voidPrint () {
System. out. println ("with"+Ink. GetColor () +"\ t Color of the cartridge in the "+Paper. Getpaper () +"\ t type of paper print out Wednesday go out and play! Nothing wrong. ");
}
PublicInk Getink () {
returnInk;
}

Public voidSetink (Ink Ink) {
This.Ink= Ink;
}
PublicPaper Getpaper () {
returnPaper;
}
Public voidSetpaper (Paper Paper) {
This.Paper= paper;
}
}

Configuration file:

<!--IOC--
<Bean id= "Happyservice" class= "Cn.happy.Service.HappyService " >
<!--DI Property value Dependency Injection --
< Property name= "Info" value= "Spring"></ Property >
</Bean>
<!--This is the color cartridge --
<Bean id= "Colorink" class= "Cn.happy.printer.ink.ColorInk" ></Bean>

<!--This is paper --
<BeanID= "B5paper"class= "Cn.happy.printer.paper.B5Paper"></Bean>
<BeanID= "A4paper"class= "Cn.happy.printer.paper.A4Paper"></Bean>

<!--This is the printer --
 <BeanID= "Pinter"   class= "Cn.happy.printer.print.Printer">
< Propertyname= "Ink"ref= "Colorink"></ Property>
< Propertyname= "paper"ref= "B5paper"></ Property>
</Bean>

Test class:

Public class SpringTest0723 {
@Test
Public void test02 () {
ApplicationContext ct= New classpathxmlapplicationcontext ("Applicationcontext.xml");
Printer pp = (Printer) Ct.getbean ("Pinter");
Pp.print ();
}

Test results:

Spring inversion of control

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.