Spring容器中Bean的生命周期 | 8lovelife's life
0%

Spring容器中Bean的生命周期

圣诞节在家整理了一下Spring容器中的Bean到底是怎么诞生的,又是如何毁灭的

容器中Bean的生命周期

下图为Spring容器中Bean的生命周期

image

容器中Bean的创建过程

下图是Spring容器创建一个Bean大致的过程

image

MergedBeanDefinitionPostProcessor#postProcessMergedBeanDefinition

下图是 CommonAnnotationBeanPostProcessor / AutowiredAnnotationBeanPostProcessor 在此刻做的事情

image

CommonAnnotationBeanPostProcessor#postProcessMergedBeanDefinition

  1. 将@PostConstruct方法缓存至lifecycleMetadataCache(LifecycleMetadata的initMethods)
  2. 将@PreDestroy方法缓存至lifecycleMetadataCache (LifecycleMetadata的destroyMethods)
  3. 将@Resouorce、@WebServiceRef、@EJB 修饰的字段或方法缓存至injectionMetadataCache(ResourceElement/WebServiceRefElement/EjbRefElement)

AutowiredAnnotationBeanPostProcessor#postProcessMergedBeanDefinition

将@Autowired、@Value、@javax.inject.Inject 修饰的字段或方法缓存至 injectionMetadataCache(AutowiredFieldElement/AutowiredMethodElement)

InstantiationAwareBeanPostProcessor#postProcessProperties

下图是 CommonAnnotationBeanPostProcessor / AutowiredAnnotationBeanPostProcessor 在此刻做的事情

image

CommonAnnotationBeanPostProcessor#postProcessProperties

获取injectionMetadataCache中的InjectedElement(ResourceElement/WebServiceRefElement/EjbRefElement )
并进行解析注入

AutowiredAnnotationBeanPostProcessor#postProcessProperties

获取injectionMetadataCache中的InjectedElement(AutowiredFieldElement/AutowiredMethodElement)
并进行解析注入

BeanPostProcessor#postProcessBeforeInitialization

下图是 CommonAnnotationBeanPostProcessor 在此刻做的事情

iamge

@PostConstruct方法此刻被调用

AbstractAutowireCapableBeanFactory#invokeAwareMethods

做的事情如图

iamge

  1. BeanNameAware.setBeanName
  2. BeanClassLoaderAware.setBeanClassLoader
  3. BeanFactoryAware.setBeanFactory

AbstractAutowireCapableBeanFactory#invokeInitMethods

做的事情如图

iamge

  1. 执行InitializingBean.afterPropertiesSet方法
  2. 执行invokeCustomInitMethod,即执行init-method

BeanPostProcessor#postProcessAfterInitialization

Spring中的@Async

AbstractBeanFactory#registerDisposableBeanIfNecessary

做的事情如图

image

  1. DisposableBean类型的Bean(DisposableBeanAdapter)被放至 DefaultSingletonBeanRegistry的disposableBeans中
  2. 过滤 DestructionAwareBeanPostProcessor 至 DisposableBeanAdapter的beanPostProcessors

容器中Bean的销毁过程

下图是Spring容器中的Bean销毁过程

image

AbstractApplicationContext#doClose

程序正常退出后,系统会调用此方法

DestructionAwareBeanPostProcessor#postProcessBeforeDestruction

下图是CommonAnnotationBeanPostProcessor在此刻做的事情

iamge

执行invokeDestroyMethods,即执行@PreDestroy方法

DisposableBeanAdapter#invokeCustomDestroyMethod

执行invokeCustomDestroyMethod,即执行destroy-method方法

事例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@Service
public class BeanPropertyResource {

@Override
public String toString() {
return "BeanPropertyResource Instantiation";
}
}

@Service
public class BeanPropertyAutowired {

@Override
public String toString() {
return "BeanPropertyAutowired Instantiation";
}
}


public class BeanLifeCycle implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware, BeanClassLoaderAware {

@Resource
private BeanPropertyResource beanPropertyResource;

@Autowired
private BeanPropertyAutowired beanPropertyAutowired;

public BeanLifeCycle() {
System.out.println("BeanLifeCycle");
System.out.println("without beanPropertyResource: " + beanPropertyResource);
System.out.println("without beanPropertyAutowired: " + beanPropertyAutowired);
}

@PostConstruct
public void postConstructMethod() {
System.out.println("with beanPropertyResource: " + beanPropertyResource);
System.out.println("with beanPropertyAutowired: " + beanPropertyAutowired);
System.out.println("postConstructMethod");
}

public void initMethod() {
System.out.println("init-method initMethod");
}


@Override
public void afterPropertiesSet() {
System.out.println("InitializingBean afterPropertiesSet");
}


@PreDestroy
public void preDestroyMethod() {
System.out.println("preDestroyMethod");
}

@Override
public void destroy() {
System.out.println("DisposableBean destroy");
}

public void destroyMethod() {
System.out.println("destroy-method destroyMethod");
}

@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("BeanClassLoaderAware");
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware");

}

@Override
public void setBeanName(String name) {
System.out.println("BeanNameAware");
}

}

XML中配置

<bean class="cn.oyo.trade.starter.BeanLifeCycle" init-method="initMethod" destroy-method="destroyMethod" />

I’m not a freak, I’m just different. - Mr. White

Reservoir Dogs