Several ways of Spring dependency injection are elaborated

  • 2020-06-07 04:28:04
  • OfStack

IoC profile

In normal Java development, programmers need to rely on the methods of other classes in one class.

The problem with new1's development is that the class instance of new is not managed by one.

Spring proposes the idea of dependency injection, in which a dependent class is not instantiated by a programmer, but is used by the Spring container to specify instances of new and inject them into the classes that need the object.

Another term for dependency injection is inversion of control. We usually have one instance of new1, and the control of this instance is ours.

Inversion of control means that the new instance work is not done by us programmers but by the Spring container.

There are several forms of dependency injection available for Spring, and this article only covers how Spring USES xml for IOC configuration.

1. Set injection

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

Then write the xml file of spring, < bean > The name attribute in class is an individual name of the class attribute, and the class attribute refers to the full name of the class, since there is a common attribute Springdao in SpringAction, it must be in the < bean > Create 1 in the tag < proprty > The tag specifies SpringDao. < property > name in the tag is the name of the SpringDao attribute in the SpringAction class, ref below < bean name="springDao"... > , spring instantiates the SpringDaoImpl object and calls the setSpringDao method of SpringAction 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 the XML file < property > The form of, but use < constructor-arg > The tag, ref attribute also points to others < bean > The 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, calls 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 ES133en-ES134en ="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 for 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 from the instance factory class:


public class SpringAction { 
// Injection of objects  
private FactoryDao factoryDao; 

public void factoryOk(){ 
factoryDao.saveFactory(); 
} 

public void setFactoryDao(FactoryDao factoryDao) { 
this.factoryDao = factoryDao; 
} 
} 

Finally, look at the spring configuration file:


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); 
} 
} 
0

5. To summarize

Spring IOC injection method is most used in (1)(2), more write more practice will be very skilled.

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


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); 
} 
} 
1

Related articles: