spring four dependency injection methods are described in detail

  • 2020-06-03 06:36:02
  • OfStack

Ordinary java development, programmers need to rely on other classes of methods in a class, is usually new1 a dependency on the class to call the class instance method, the problems existing in the development are new class instance is bad management system 1, spring puts forward the idea of dependency injection, which depend on the class is not programmers instantiated, but through spring container and will help us new specified instance instance into need the object class. Dependency injection is also known as Inversion of Control (IOC), which means that the new instance is not done by us programmers but by the spring container.

spring comes in many forms of dependency injection, but here's how spring USES xml to configure IOC:

1. Set injection

This is the simplest method of injection. Assuming you have 1 SpringAction and you need to instantiate 1 SpringDao object in your class, you can define 1 private SpringDao member variable and then create the set method of SpringDao (which is the injection entry for ioc) :


package com.bless.springdemo.action; 
public class SpringAction { 
    // Injection of objects springDao 
  private SpringDao springDao; 
    //1 Must write to the injected object set methods  
    public void setSpringDao(SpringDao springDao) { 
    this.springDao = springDao; 
  } 
 
    public void ok(){ 
    springDao.ok(); 
  } 
} 

Then I wrote the xml file of spring, < bean > The name attribute in SpringAction is an individual name of the class attribute, and the class attribute refers to the full name of the class, since there is a public attribute Springdao in SpringAction, so it must be in the < bean > Create 1 in the tag < property > The tag specifies SpringDao. < property > name in the tag is the SpringDao attribute name in the SpringAction class, ref below < bean name="springDao"... > , spring instantiates the SpringDaoImpl object and calls SpringAction's setSpringDao method to inject SpringDao:


<!-- configuration bean, The class is configured by spring management --> 
  <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 
    <!--(1) Dependency injection , Configure the corresponding properties in the current class --> 
    <property name="springDao" ref="springDao"></property> 
  </bean> 
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl">
</bean> 

2. Constructor injection

This way refers to the constructor with parameters of the injection of injection, look at the following example, I created two member variable SpringDao and User, but did not set the set method of object, so can't support 1 kind of injection pattern, injection here is in SpringAction constructor injection, that is to say, when creating SpringAction objects will declare SpringDao and User two parameter values come in:


public class SpringAction { 
  // Injection of objects springDao 
  private SpringDao springDao; 
  private User user; 
   
  public SpringAction(SpringDao springDao,User user){ 
    this.springDao = springDao; 
    this.user = user; 
    System.out.println(" Constructor call springDao and user"); 
  } 
     
    public void save(){ 
    user.setName(" kaka "); 
    springDao.save(user); 
  } 
} 

It is also not used in XML < property > The form of, but use < constructor-arg > The tag, ref attribute also points to others < bean > name attribute of the tag:


<!-- configuration bean, The class is configured by spring management --> 
  <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 
    <!--(2) Create constructor injection , You need to add this configuration if the main class has a parameterized constructor --> 
    <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>

To resolve the uncertainty of constructor parameters, you may encounter two parameters of the same type passed in by the constructor. In order to figure out which one to assign the corresponding value, you need to do a little processing:

Here is setting index, which is the parameter position:


<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 parameter types:


<constructor-arg type="java.lang.String" ref=""/> 

3, static factory method injection

Static factory, as the name implies, is to call the methods of static factory to get the objects we need. In order for spring to manage all objects, we cannot directly get objects through "engineering class.static method ()", but still get objects through the form of spring injection:


package com.bless.springdemo.factory; 
 
import com.bless.springdemo.dao.FactoryDao; 
import com.bless.springdemo.dao.impl.FactoryDaoImpl; 
import com.bless.springdemo.dao.impl.StaticFacotryDaoImpl; 
 
public class DaoFactory { 
  // Static factory  
  public static final FactoryDao getStaticFactoryDaoImpl(){ 
    return new StaticFacotryDaoImpl(); 
  } 
} 

Also looking at the key class, here I need to inject 1 FactoryDao object. This looks like the first injection of 1 module 1, but look at the following xml and you will see a big difference:


 public class SpringAction { 
    // Injection of objects  
  private FactoryDao staticFactoryDao; 
   
  public void staticFactoryOk(){ 
    staticFactoryDao.saveFactory(); 
  } 
  // Injected object's set methods  
  public void setStaticFactoryDao(FactoryDao staticFactoryDao) { 
    this.staticFactoryDao = staticFactoryDao; 
  } 
} 

IOC configuration file for Spring, take a look < bean name="staticFactoryDao" > Instead of pointing to the implementation class of FactoryDao, the class is pointing to static factory DaoFactory and configuring ES128en-ES129en ="getStaticFactoryDaoImpl" to specify which factory method to call:


<!-- configuration bean, The class is configured by spring management --> 
  <bean name="springAction" class="com.bless.springdemo.action.SpringAction" > 
    <!--(3) Inject objects using static factory methods , This corresponds to the following configuration file (3)--> 
    <property name="staticFactoryDao" ref="staticFactoryDao"></property> 
        </property> 
  </bean> 
  <!--(3) The way to get the object here is to get the static method from the factory class --> 
  <bean name="staticFactoryDao" class="com.bless.springdemo.factory.DaoFactory" factory-method="getStaticFactoryDaoImpl"></bean> 

4. Method injection of the instance factory

The instance factory means that the method to get an instance of an object is not static, so you need to first call the new factory class and then call the normal instance method:


public class DaoFactory { 
  // Instance factory  
  public FactoryDao getFactoryDaoImpl(){ 
    return new FactoryDaoImpl(); 
  } 
} 

There is nothing to say about the following class, which is similar to the previous one, but we need to create the FactoryDao object with the instance factory class:


<!-- configuration bean, The class is configured by spring management --> 
  <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 
    <!--(1) Dependency injection , Configure the corresponding properties in the current class --> 
    <property name="springDao" ref="springDao"></property> 
  </bean> 
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl">
</bean> 
0

Finally, look at the spring configuration file:


<!-- configuration bean, The class is configured by spring management --> 
  <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 
    <!--(1) Dependency injection , Configure the corresponding properties in the current class --> 
    <property name="springDao" ref="springDao"></property> 
  </bean> 
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl">
</bean> 
1

conclusion

Spring IOC Injection method is used most (1)(2), thanks for more practice will be very skilled.

Also note: Objects created through Spring are singletons by default, if you need to create multiple instance objects you can do so in < bean > Add 1 attribute after the tag:


<!-- configuration bean, The class is configured by spring management --> 
  <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 
    <!--(1) Dependency injection , Configure the corresponding properties in the current class --> 
    <property name="springDao" ref="springDao"></property> 
  </bean> 
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl">
</bean> 
2

Related articles: