Dependency injection is a process in which the dependent objects are injected dynamically into the component by an external container, and the common understanding is that we are the programmers of the new instance, and control inversion means that the new instance works not by our programmers but by the spring container.
Spring has several forms of dependency injection, and the following is a description of how Spring uses XML for IOC configuration:
1.set Injection
This is the simplest way to inject, assuming that there is a personservicebean, the class needs to instantiate a Persondao object, then you can define a private Persondao member variable, Then create the Persondao set method (this is the input entry for the IOC):
1 PublicClass personservicebean{2 //Inject Object Persondao3 PrivatePersondao Persondao;4 //setter methods for injected objects5 Public voidSetpersondao (Persondao Persondao) {6 This. Persondao =Persondao;7 }8 Public voidSave (person person) {9 Persondao.save (person);Ten } One}
The spring XML file is then written, andthe name attribute in <bean> is an alias for the Class property, which refers to the full name of the classes because there is a public property in Personservicebean Persondao , so create a <property> tag in the <bean> tab to specify Persondao. The name in the <property> tag is the Persondao attribute name in the Personservicebean class, and ref refers to the following <bean name= "Persondao" ...; This is actually a spring that instantiates the Persondaoimpl object and calls the Personservicebean Setpersondao method to inject Persondao
Beans.xml configuration:
1 <!--config bean, configured with spring management and2class = "Cn.itcast.dao.impl.SpringDaoBean" > </bean>3class= "Cn.itcast.service.impl.PersonServiceBean" >4 <!--(1) Dependency injection, configuring the corresponding property in the current class--5 <property name= "Persondao" ref= "Persondao" ></property>6 </bean>
2. Constructor injection
This method of injection refers to the constructor injection with parameters, see the following example, I created two member variables Persondao and user, but did not set the object's set method, so it cannot support the first injection method, The injection method here is injected into the Personservicebean constructor, meaning that Persondao and user two parameter values are passed in when creating the Personservicebean object:
1 Public classpersonservicebean{2 PrivatePersondao Persondao;3 Privateuser user;4 PublicPersonservicebean (Persondao persondao,user User) {5 This. Persondao =Persondao;6 This. user =user;7System.out.println ("Construct method call Persondao,user object");8 } 9 Public voidSave () {TenUser.setname ("haha"); One persondao.save (user); A } -}
In the XML file It is also not in the form of <property>, but instead of the <constructor-arg> tag, the ref attribute also points to the name attribute of the other <bean> tag:
1 <!--configuration Bean, which is configured with spring management and2 class= " Cn.itcast.service.impl.PersonServiceBean ">3 <!--(2) Create a constructor injection, and if the main class has a constructor with a parameter, you need to add this configuration--4 <constructor-arg ref= "Persondao" ></constructor-arg>5 <constructor-arg ref= "User" ></constructor-arg>6 </bean>7 class= " Cn.itcast.dao.impl.personDaoBean "></bean>8 class=" Cn.itcast.vo.User "> </bean>
Spring Dependency Injection method and principle