In the normal Java development, programmers in a class need to rely on other classes of methods, it is usually a new dependent class to invoke the class instance method, the problem is that the new class instance is not a unified management, spring put forward the idea of dependency injection, that is, the dependency class is not instantiated by the programmer, Instead, the spring container helps us to specify the instance and inject the instance into the class that needs the object. Another argument for dependency injection is "inversion of control", the popular understanding is that we are usually the new instance, the control of this instance is our programmer, and control inversion means that the new instance work is not done by our programmers but by the spring container.
Spring has several forms of dependency injection, and here's just how spring uses XML for IOC configuration: One: Set injection This is the simplest way to inject, suppose there is a springaction, the class needs to instantiate a Springdao object, You can then define a private Springdao member variable and then create the Springdao set method (which is the input entry for the IOC): Java code package com.bless.springdemo.action; public class springaction { //Injection Object springdao private springdao springDao; //Be sure to write the set method of the injected object public void setspringdao (SpringDao springDao) { this.springDao = springDao; } public void ok () { springdao.ok (); } &nbSp }
The name attribute in the XML file,<bean>, which is then written in spring, is an alias for the Class property, which refers to the full name of the classes, because there is a public property Springdao in Springaction, so the <bean > Create a <property> tag in the label to specify Springdao. The name in the <property> tag is the Springdao attribute name in the Springaction class, and ref refers to the following <bean name= "Springdao" ...; This is in fact spring to instantiate the Springdaoimpl object and call Springaction's Setspringdao method to inject Springdao: Java code <!--configuration Bean, which is managed by spring after configuration-- > <bean name= "springaction" class= "Com.bless.springdemo.action.SpringAction" > <!--(1) Dependency injection, configuring the current The corresponding property in class--<property name= "Springdao" ref= "Springdao" ></property> </bean> <bean Name= "Springdao" class= "Com.bless.springdemo.dao.impl.SpringDaoImpl" ></bean>
Second: constructor injection in this way refers to the constructor injection with parameters, see the following example, I created two member variables Springdao 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 springaction constructor, meaning that Springdao and user two parameter values are passed in when creating the Springaction object: Java code public class springaction { //Injection Object springdao private SpringDao springDao; private User user; public springaction ( Springdao springdao,user user) { this.springdao = springdao; this.user = user; system.out.println (" Construct method call Springdao and user "); } public void save () { User.setname ("Kaka"); springdao.save (user); } } In the XML file is also not in the form of <property>, but using the < Constructor-arg> tag, the ref attribute also points to the Name property of the other <bean> tag: XML code <!--configuration Bean, which is configured with spring-managed <bean name= "Springaction" class= " Com.bless.springdemo.action.SpringAction "> <! --(2) Create constructor injection, add this configuration if the main class has a constructor with parameters--> < constructor-arg ref= "Springdao" ></constructor-arg> <constructor-arg ref= "User" ></constructor-arg> </bean> <bean name= "Springdao" class= "Com.bless.springdemo.dao.impl.SpringDaoImpl" > </bean> <bean name= "User" class= "Com.bless.springdemo.vo.User" ></bean> Solving the uncertainties of the constructor method parameters, you may encounter the constructor method in which both parameters are the same type, In order to distinguish which one should be assigned the corresponding value, some minor processing is required: The following is the setting index, which is the parameter location: XML code <bean name= "Springaction" class= " Com.bless.springdemo.action.SpringAction "> < constructor-arg index= "0" ref= "Springdao" ></constructor-arg> <constructor-arg index= "1" ref= "user" ></constructor-arg> </bean> The other is setting the parameter type: XML code
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.