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