How many of these ways Spring Ioc instantiates Bean objects, do you know?

For those who need more related information, please see the profile on the homepage!

Spring's way of instantiating beans

  • Constructor method
  • Static factory
  • Instantiated factory

Case practice

Instantiate bean object by way of constructor

<bean id="hello" name="hello" class="com.xxx.demo.Hello"></bean>

**The empty constructor created by the default constructor must exist or the creation will fail**

Static factory mode

Features:

Must have the factory class and factory method

The factory method is static

StaticFactory

/**
 * 静态工厂模式
 *
 */
public class StaticFactory {
    
    
	public static GoodsService createGoodsService() {
    
    //方法一定要是一个静态方法
		return new GoodsService();
	}
}

GoodsService entity class

public class GoodsService {
    
    
	public void getGoodsInfo() {
    
    
		System.out.println("外星人贼便宜");
	}
}

Bean configuration

<bean id="goodsService" 
		class="com.xxx.demo.StaticFactory" factory-method="createGoodsService"></bean>

When we specify Spring to use a static factory method to create a Bean instance, Spring will first parse the configuration file, and according to the information specified in the configuration file, ** call the static factory method of the static factory class through reflection and return the static factory method The value is used as a Bean instance. In this process, Spring is no longer responsible for creating Bean instances. Bean instances are provided by static factory methods provided by users.

Create Bean by instantiating factory

Compared to static factory implementation

1. The factory method is a non-static method

2. The factory bean needs to be configured, and the factory-bean and factory-method attributes are configured in the business bean

Instantiate factory definition

/**
 * 实例化工厂
 * @author Best Liu
 *
 */
public class InstanceFactory {
    
    
	public OrderService createOrderService() {
    
    
		return new OrderService();
	}
}

Entity class definition

public class OrderService {
    
    
	public void getOrderInfo() {
    
    
		System.out.println("亲,已经下单完成,但是想发货没门");
	}
}

Bean configuration

<!-- 
	实例化工厂 
	1、定义实例化工厂bean
	2、引用工厂bean指定工厂创建方法(方法为非静态)
-->
	<bean id="instanceFactory" class="com.xxx.demo.InstanceFactory"></bean>
	<bean id="orderService" factory-bean="instanceFactory" factory-method="createOrderService"></bean>

Expand

Comparison of Spring's three ways of instantiating beans

Method 1: ** Created by the default constructor of the bean, ** can be used when the business logic of each bean is relatively independent of each other or less connected to the outside world.

Method 2: Use the static factory method to create, you can manage the creation of each bean in a unified manner. If each bean needs the same initialization process before being created, you can use this factory method to perform unified processing first.

Method 3: Use the instantiated factory method to create, that is, the factory method is also controlled as a business bean,

1. Bean creation management methods that can be used to integrate other frameworks

2. The role of bean and factory can be interchanged

Projects in development generally use one method to instantiate beans. Project development basically uses the first method, which is managed by spring, and can be used directly when using it, and the other two can be understood.

Guess you like

Origin blog.csdn.net/xyx12321/article/details/111283972
Recommended