What are the several ways to implement instantiation in the Spring framework?

Three ways to implement instantiation in the Spring framework:

Type 1: Use the constructor to instantiate the Bean

This is the simplest way. The Spring IoC container can either use the default empty constructor or the parameterized constructor to create Beans.
 

The second kind: use static factory method to instantiate Bean

In addition to specifying the required class attribute, use this method to specify the factory-method attribute to specify the method of instantiating the Bean, which must be a static method. And using the static factory method also allows you to specify method parameters, and the spring IoC container will call the method specified by this attribute to obtain the Bean.
 

Type 3: Use the instance factory method to instantiate the Bean

In this way, the class attribute cannot be specified. At this time, the factory-bean attribute must be used to specify the id of the factory Bean. The factory-method attribute specifies the method of instantiating the Bean, and the use of the instance factory method allows specifying method parameters, methods and using the constructor The same way.

Guess you like

Origin blog.csdn.net/cz_00001/article/details/112527355