The way Spring instantiates beans

  • Constructor

  • Static factory mode

  • Example chemical plant mode

A case in field

Constructor to instantiate the bean object

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

The empty constructor created with the default constructor must exist or the creation fails

Static factory mode

Features:

Have the factory class and factory method

The factory method is static

StaticFactory indicates the StaticFactory

Public StaticFactory {public static GoodsService createGoodsService() {return static GoodsService createGoodsService()  GoodsService(); }}Copy the code

GoodsService entity class

Public class GoodsService {public void getGoodsInfo() {system.out.println (" alien "); }}Copy the code

Bean configuration

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

When we specify that Spring uses static factory methods to create Bean instances, Spring first parses the configuration file and, based on the information specified in the configuration file, ** calls the static factory method of the static factory class through reflection and takes the return value of the static factory method as the Bean instance. ** In this process, Spring is no longer responsible for creating Bean instances, which are provided by static factory methods provided by the user.

Create the Bean in a chemical instance

Compare this to a static factory implementation

1. The factory method is a non-static method

2. You need to configure the factory bean and configure the factory-bean and factory-method properties in the business bean

Example Chemical Plant Definition

@author Best Liu ** /public class InstanceFactory {public OrderService createOrderService() {return new OrderService(); }}Copy the code

Entity Class Definition

Public class OrderService {public void getOrderInfo() {system.out.println (); }}Copy the code

Bean configuration

<! <bean id="instanceFactory" > <bean id="instanceFactory" class="com.xxx.demo.InstanceFactory"></bean> <bean id="orderService" factory-bean="instanceFactory" factory-method="createOrderService"></bean>Copy the code

extension

Compare the three ways Spring instantiates beans

Method 1: ** is created using the bean’s default constructor. ** Can be used when the business logic of the individual beans is relatively independent of each other or less associated with the outside world.

Method 2: the use of static factory method to create, you can unified management of the creation of each bean, such as each bean before the creation of the same initialization process, you can use the factory method for unified processing and so on.

Method 3: use the instantiation factory method to create, that is, factory method also as a business bean to control,

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

2. Be able to switch the roles of bean and factory

In development, projects generally use one way to instantiate beans. Project development basically adopts the first way, which is handed over to Spring for hosting and used directly, and the other two ways can be understood.