How Spring instantiates beans

  • The Set injection
  • Constructor injection
  • Static factory injection
  • Example chemical plant injection

A case in field

The Set injection

XML configuration (Spring also provides set injection for basic data types)

< property name=”userDao” ref=”userDao”>

UserDao.java

Public class userDao {public String userLogin() {return “I am a userDao “; }}

UserService.java

Public class UserService {public class UserService {private UserDAO UserDAO;

public void userlogin() { String res=userDao.userLogin(); System.out.println(res); } public void setUserDao(UserDao userDao) { this.userDao = userDao; }}

App.java

import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“spring-config.xml”); UserService userService=applicationContext.getBean(“userService”, UserService.class); userService.userlogin(); }}

Constructor injection

XML configuration (also provides injection for basic data types, string equivalents)

< bean id = “userServiceV2” class = “com. XXX. Demo. UserServiceV2” > < constructor – arg index = “0” Ref =”userDao”

Constructor injection comes in three forms:

The index property is in the order of parameters. If there is only one parameter, the index property can not be set.

The name attribute is based on the name of the attribute in the constructor.

The type property is matched according to the type of the property in the constructor. If the attributes of the same type are not unique, the injected attributes are injected in order.

The UserServiceV2.java class provides the constructor

/ * *

  • Implement constructor injection
  • @author Best Liu

* */ public class UserServiceV2 { private UserDao userDao; private String name; public void userlogin() { String res=userDao.userLogin(); System.out.println(res); System.out.println(name); } public UserServiceV2(UserDao userDao,String name) { super(); this.userDao = userDao; this.name = name; }}

Static factory injection

The XML configuration

< bean id = “userDao01” class = “com. XXX. Demo. StaticFactory” factory – method = “createuserDao” > < / bean > < bean id=”userService01″ class=”com.xxx.demo.UserService”>

StaticFactory.java

public class StaticFactory { public static UserDao createuserDao(){ return new UserDao(); }}

UserService.java

public class UserService {

private UserDao userDao;

public void userlogin() { String res=userDao.userLogin(); System.out.println(res); } public void setUserDao(UserDao userDao) { this.userDao = userDao; }}

Tips: Static factory injection is the setter injection of the IOC static factory and DI. The property object that needs to be injected is created using the static factory.

2.4 Instance plant

The XML configuration

< bean id = “instanceFactory” class = “com. XXX. Demo. InstanceFactory” > < / bean > < bean id = “userDao3″ factory-bean=”instanceFactory” factory-method=”createUserDao”>

InstanceFactory.java

public class InstanceFactory { public UserDao createUserDao(){ return new UserDao(); }}

Tips: Focus on mastering SET, constructor injection, factory mode can be understood, the actual development of the basic use of SET mode injection beans.

extension

The problem of circular dependence

Beans are injected through constructors, and they depend on each other so that the beans cannot be instantiated.

Choice of injection: Set mode injection is preferred in development projects

Injection can use structure in building of objects at the same time as the establishment of complete dependency on the object the establishment of everything is ready, but if you want to build a lot of object relations, use a structure will be in a long series of building function parameters, and not easy to memory, then using the Set injection will be a good choice.

Using Set injection gives you a clear name to know what the injected object will be, and a name like setXXX () is better than remembering that the position of an argument on your Constructor represents an object.

The XML configuration

–> –>

GoodsService.java

public class GoodsService { private UserService userService; /*public GoodsService(UserService userService) { super(); this.userService = userService; }*/ public void setUserService(UserService userService) { this.userService = userService; }}

UserService.java

public class UserService { private GoodsService goodsService; /* public UserService(GoodsService goodsService) { super(); this.goodsService = goodsService; } */ public void setGoodsService(GoodsService goodsService) { this.goodsService = goodsService; }}