【Spring源码解析】生命周期-BeanPostProcessor-后置处理器

前言

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization..." + beanName);
return bean;
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization..." + beanName);
return bean;
}
}
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
/**
* postProcessBeforeInitialization...beanConfigOfLifeCycle
* postProcessAfterInitialization...beanConfigOfLifeCycle
* cat constructor...
* postProcessBeforeInitialization...cat
* cat init...
* postProcessAfterInitialization...cat
* dog constructor...
* postProcessBeforeInitialization...dog
* dog init...
* postProcessAfterInitialization...dog
* car constructor...
* postProcessBeforeInitialization...car
* car init...
* postProcessAfterInitialization...car
* 容器创建完成
* car destory...
* dog destroy...
* cat destroy...
*/
@Test
public void test07() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanConfigOfLifeCycle.class);
System.out.println("容器创建完成");

// 关闭容器
applicationContext.close();
}

最后

本篇到此结束,欢迎大家关注公众号【当我遇上你】。