One: Here first, Di (Dependency injection) Dependency injection has a form of expression: one is the CI (Constructor Injection) Construction method injection, the other is SI (set injection) set injection. This essay is about the first method of structural injection (Constructor injection).
In fact di (Dependency injection) Dependency injection you might as well read it backwards: Injecting dependency is the injection of "dependency" into an object. So what is "dependency"? Dependency means that when an object is initialized or instantiated, another object is required, and we make the other object "dependent." such as the construction method injection (Constructor injection) is to the dependency as a construction method of the parameters injected into. Such as:
Injection B to A. We call B dependency. In general, B is an interface that conforms to the specification for interface-oriented programming.
Public A (b b) {
}
Two: What are the benefits of using dependency injection?
The greatest benefit of the 1.DI (Dependency injection) is decoupling (decoupling). As the above code injects B to a if B is an interface or abstract class. As long as any one class implementation or inheritance B can be injected to a. Therefore, the purpose of decoupling can be achieved.
2. Who does the dependency inject to?
Dependencies injected into the objects that need them. (dependency is injected to those who need them.) )
3. How to get dependencies into a dependent object in spring (see the code above: How do I get B into a)?
Here I quote a text from spring in action: (Note: My sample code below is basically the same as spring in action)
The big question here is,how can-give slaydragonquest to Braveknight? And how can I give a printstream to Slaydragonquest?
The act of creating associations between application components are commonly referred to as wiring.
Spring configures the configuration file, configures the bean form, completes the assembly, and assembles the implementation to inject the dependency into the object that needs them.
"I also want to explain here that a bean in the config file actually corresponds to a javabean, which is a Pojo object, and the bean is the abbreviated form of JavaBean."
The directory structure of the sample code is as follows:
The two-lap configuration file above would like to illustrate two issues:
One is: New Classpathxmlapplicationcontext ("Spring-julysecond.xml");//Note that the. xml file is in the SRC directory.
New Classpathxmlapplicationcontext ("Com/config/spring-julythird.xml")//Note this. xml file under Com.cofig this file.
Another problem is the decoupling of Di: Slaydragonquest is injected into the Spring-julysecond.xml file, Spring-julythird.xml injected is searchhillquest. Without changing the code, look at the implementation class of the incoming quest, and the results are different.
The code for Spring-julysecond.xml is as follows:
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4Xsi:schemalocation= "Http://www.springframework.org/schema/beans5http//www.springframework.org/schema/beans/spring-beans.xsd ">6<!-- Thisis constructor injection-->7<!--The act of creating associations between application components are commonly referred to8As Wiring-->9<bean id= "Knight"class= "Com.qls.impl.BraveKnight" >Ten<!--collaborators and configuration for ThisBean go here-- One<constructor-arg ref= "Query" ></constructor-arg> A</bean> -<bean id= "Query"class= "Com.qls.impl.SlayDragonQuest" > -<!--collaborators and configuration for ThisBean go here-- the<constructor-arg value= "#{t (System). Out}"/> -</bean> -<!--more beans definitions Go -</beans>
The code for Spring-julythird.xml is as follows:
1<?xml version= "1.0" encoding= "UTF-8"?>2<beans xmlns= "Http://www.springframework.org/schema/beans"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4Xsi:schemalocation= "Http://www.springframework.org/schema/beans5http//www.springframework.org/schema/beans/spring-beans.xsd ">6<!-- Thisis constructor injection-->7<!--The act of creating associations between application components are commonly referred to8As Wiring-->9<bean id= "Knight"class= "Com.qls.impl.BraveKnight" >Ten<!--collaborators and configuration for ThisBean go here-- One<constructor-arg ref= "Query" ></constructor-arg> A</bean> -<bean id= "Query"class= "Com.qls.impl.SearchHillQuest" > -<constructor-arg value= "#{t (System). Out}"/> the</bean> -<!--more beans definitions Go -</beans>
The code for the query interface is as follows:
1 PackageCom.qls.inter;2 3 /**4 * Created by ${Quinlinson} on 2017/6/2.5 */6 Public InterfaceQuery {7 voidquery ();8 voidembark ();9 }Ten One
The code for Knight is as follows:
1 Package Com.qls.inter; 2 3 /** 4 * Created by ${Quinlinson} on 2017/6/2. 5 */ 6 Public Interface Knight {7 void embarkonquest (); 8 }
The code for the implementation class of the query interface is as follows:
The first is: Slaydragonquest code is as follows:
1 PackageCom.qls.impl;2 3 ImportCom.qls.inter.Query;4 5 ImportJava.io.PrintStream;6 7 /**8 * Created by ${Quinlinson} on 2017/6/2.9 */Ten Public classSlaydragonquestImplementsquery{ One PrivatePrintStream stream; A - Publicslaydragonquest (PrintStream stream) { - This. Stream =stream; the } - @Override - Public voidEmbark () { -Stream.println ("Embarking on quest to slay the dragon"); +Stream.println ("Ouyangfeng is the very beautiful woman in the world."); - } + @Override A Public voidquery () { atSystem.out.println ("Slay Dragon Quest"); - } -}
The following is the code for Searchhillquest:
1 PackageCom.qls.impl;2 3 ImportCom.qls.inter.Query;4 5 ImportJava.io.PrintStream;6 7 /**8 * Created by ${Quinlinson} on 2017/6/3.9 */Ten Public classSearchhillquestImplementsquery{ One PrivatePrintStream stream; A - Publicsearchhillquest (PrintStream stream) { - This. Stream =stream; the } - - @Override - Public voidquery () { + - } + A @Override at Public voidEmbark () { -Stream.println ("The Hero Search the hill where the damsel was missing"); - } -}
The code for Braveknight is as follows:
1 PackageCom.qls.impl;2 3 ImportCom.qls.inter.Knight;4 ImportCom.qls.inter.Query;5 Importorg.springframework.context.annotation.Configuration;6 7 /**8 * Created by ${Quinlinson} on 2017/6/2.9 */Ten One Public classBraveknightImplementsknight{ A Privatequery query; - - Publicbraveknight (query query) { the This. query =query; - } - - @Override + Public voidembarkonquest () { - Query.embark (); + } A}
The code for the test class is as follows:
1 Packagecom.qls.test;2 3 ImportCom.qls.inter.Knight;4 ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;5 6 /**7 * Created by ${Quinlinson} on 2017/6/2.8 */9 Public classTest {Ten Public Static voidMain (string[] args) { One /** A * How to load the configuration file: directly write the name of the configuration file under SRC, - * Do not add the previous package name directly under SRC. The front of the package name can be added without slashes/ - */ the //Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("/com/config/spring-julythird.xml" ); - //Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext ("Com/config/spring-julythird.xml") ; -Classpathxmlapplicationcontext context =NewClasspathxmlapplicationcontext ("Spring-julysecond.xml"); -Knight Knight = Context.getbean (Knight.class); + knight.embarkonquest (); - context.close (); + A } at}
Spring in Action learning Note one: DI (Dependency injection) Dependency Injection CI (Constructor injection) constructor injection