Ioc:inverse of control: controlling inversion.
This means that the relationship between the programs, without code control, is entirely controlled by the container. At run time, the container injects their relationships directly into the component based on the configuration information. Again, this is the implication of dependency injection. Dependency injection and control inversion are actually a concept. But the emphasis is on the difference, the injection of dependency injection is done by the container at runtime, while the control inversion emphasizes that the relationship is controlled by the container. In fact, the essence is the same.
Post a piece of code
/** * School Class */public class School {private String name; Public School (String name) { this. name=name; } Public void Printinfo () { System.out.println ("The name of the school is:" +name);} }
/*** Student Class*/ Public classStudent { Public intID; PublicString name; PrivateSchool School; Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicSchool Getschool () {returnSchool; } Public voidSetschool (School School) { This. School =School; } }
<?XML version= "1.0" encoding= "UTF-8"?> <Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/s Pring-aop-2.0.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring -tx-2.0.xsd "> <BeanID= "School"class= "Ioc.iocsample.School"> <Constructor-argIndex= "0"> <value>Langfang Teachers College</value> </Constructor-arg> </Bean> <BeanID= "Student"class= "Ioc.iocsample.Student"> < Propertyname= "id"value= "001"/> < Propertyname= "Name"value= "Zhang San"/> < Propertyname= "School"ref= "School"/> </Bean> </Beans>
Importorg.springframework.beans.factory.BeanFactory; ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classClient { Public Static voidMain (string[] args) {//TODO auto-generated Method StubBeanfactory factory =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Student Student= (Student) factory.getbean ("Student"); Student.getschool (). Printinfo (); } }
There are three types of dependency injection Di (dependence injection) in spring: The first is interface injection (Interface injection) and the second is Get set injection (set/get Injection) The third type is constructor injection (Constructor injection)
Three different ways to inject:
1. Interface injection: Components need to rely on the implementation of a specific interface, where the loading interface implementation and interface implementation of the specific objects are done by the container. In this way, the interface must be dependent on the container, such that the component is intrusive and reduces reusability. Context.lookup (SERVLETCONTEXT.GETXXX), which is commonly used in the development of Java EE, is the expression of interface injection. (This type of injection is not commonly used)
2.getter/setter Mode injection: It is more clear what needs to be injected. Java-compliant design rules. More suitable for Java developers, more natural and more convenient to use.
3. Constructor mode injection: When the class is loaded, the dependent components are already injected. But if there are many parameters, it is inconvenient to use them.
However, the latter two injection methods are commonly used in spring, and the first interface injection method is not commonly used.
What is the IOC (inversion of control) in spring