编程知识 cdmana.com

Knowledge sharing: how many ways does spring IOC instantiate bean objects?

Spring Instantiation bean The way

  • The way constructors are
  • Static factory mode
  • Example chemical plant mode

Case practice

The way constructors instantiate bean object

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

Create by default constructor Empty constructors must exist Otherwise, the creation fails

Static factory mode

characteristic :

It is necessary to have the type and method of the factory

The factory method is static

StaticFactory Static factory

/**

  • Static factory mode

*
*/
public class StaticFactory {
public static GoodsService createGoodsService() {// Method must be a static method
return new GoodsService();
}
}

GoodsService Entity class

public class GoodsService {
public void getGoodsInfo() {
System.out.println(" Alien thieves are cheap ");
}
}

Bean To configure

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

When we specify Spring Use the static factory method to create Bean When an instance ,Spring The configuration file will be parsed first , And according to the information specified in the configuration file , Call the static factory method of the static factory class through reflection , And take the return value of the static factory method as Bean example , In the process ,Spring No longer responsible for creating Bean example ,Bean The instance is provided by the static factory method provided by the user .

Example chemical plant way to create Bean

Compared with static factory implementation

1、 The factory method is non static

2、 Need to configure the factory bean, And in business bean Middle configuration factory-bean,factory-method attribute

Example chemical plant definition

/**

  • Example chemical plant
  • @author Best Liu

*
*/
public class InstanceFactory {
public OrderService createOrderService() {
return new OrderService();
}
}

Entity class definition

public class OrderService {
public void getOrderInfo() {
System.out.println(" Pro - , The order has been placed , But there's no way to ship ");
}
}

Bean To configure

<!--
Example chemical plant
1、 Define the example factory bean
2、 Reference factory bean Specify the factory creation method ( Method is not static )
-->
<bean id="instanceFactory" class="com.xxx.demo.InstanceFactory"></bean>
<bean id="orderService" factory-bean="instanceFactory" factory-method="createOrderService"></bean>

Expand

Spring Three instantiations bean Method comparison

Mode one : adopt bean The default constructor for creates , When each bean It can be used when the business logic is relatively independent of each other or less related to the outside world .

Mode two : Use static factory Method creation , We can manage all of them in a unified way bean The creation of , Like each other bean The same initialization process is required before creation , You can use this factory Methods first unified processing and so on .

Mode three : Using instantiation factory Method creation , the factory Methods are also used as business bean To control ,

1、 Can be used to integrate other frameworks bean Create management methods

2、 Can make bean and factory The role of the switch

In development, a project is usually instantiated in one way bean, Project development basically adopts the first way , hand spring trusteeship , When you use it, you can use it directly , The other two kinds of understanding are enough .

版权声明
本文为[Little Tinker who loves programming]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224152603266f.html

Scroll to Top