我今天学习到SpringBoot里面自定义Bean的初始化与销毁方法
我先总结一下我学到的四种方法:
方法一:
指定init-method 和 destory-method
方法二:
通过让 Bean 实现 InitializingBean 接口,定义初始化逻辑
DisposableBean 接口,定义销毁逻辑
方法三:
用 @PostConstruct,在 Bean 创建完成并且赋值完成后,执行该注解标注的方法
@PreDestroy,在容器销毁 Bean 之前,执行该注解标注的方法
方法四:
通过让 Bean 实现 BeanPostProcessor 接口,在Bean 初始化前后进行一些处理工作
- postProcessBeforeInitialization: 在初始化之前工作
- postProcessAfterInitialization: 在初始化之后工作
然后我就在想它们的执行顺序是怎样的:
尝试一:
配置类:
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
|
//告诉Spring这是一个配置类 @Configuration public class MainConfigOfLifeCycle { //利用 init-method 和 destory-method @Bean (initMethod= "initTest" , destroyMethod= "detoryTest" ) public Car car() { return new Car(); } //实现 InitializingBean , DisposableBean 接口 @Bean public Cat cat() { return new Cat(); } //利用 @PostConstruct ,@PreDestroy @Bean public Dog dog() { return new Dog(); } //实现 BeanPostProcessor 接口 @Bean public MyBeanPostProcessor myBeanPostProcessor() { return new MyBeanPostProcessor(); } } |
4个 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
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
|
public class Car{ public void initTest() { System.out.println( " .. init-method .. " ); } public void detoryTest() { System.out.println( " .. destory-method .. " ); } } public class Cat implements InitializingBean, DisposableBean { //该Bean在销毁时,调用 public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println( " .. DisposableBean .." ); } //该Bean创建完成并且赋值完成后,调用 public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub System.out.println( " .. InitializingBean .." ); } } public class Dog { //对象创建并赋值之后调用 @PostConstruct public void init() { System.out.println( " .. @PostConstruct .. " ); } //容器移除对象之前 @PreDestroy public void detory() { System.out.println( " .. @PreDestroy .. " ); } } public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println( " .. postProcessBeforeInitialization .. " ); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // TODO Auto-generated method stub System.out.println( " .. postProcessBeforeInitialization .. " ); return bean; } } |
运行:
1
2
3
4
5
6
7
8
9
10
11
|
public class IOCTest_LifeCycle { @Test public void test01() { // 1. 创建IOC容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle. class ); System.out.println( "容器创建完成" ); // 关闭容器 applicationContext.close(); } } |
执行结果:
思考:发现容器在加载 Bean 时是顺序的,因为我在MainConfigOfLifeCycle这个配置类里 @Bean 是顺序的,所有不能确定这次结果是否准确。
尝试二:我把上面四种方法都杂糅到一个类里
配置类:
1
2
3
4
5
6
7
|
@Configuration public class MainConfigOfLifeCycle { @Bean (initMethod= "initTest" , destroyMethod= "detoryTest" ) public Car car() { return new Car(); } } |
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
public class Car implements InitializingBean, DisposableBean, BeanPostProcessor { public Car() { System.out.println( "Car 创建" ); } public void initTest() { System.out.println( " .. init-method .. " ); } public void detoryTest() { System.out.println( " .. destory-method .. " ); } @Override public void afterPropertiesSet() throws Exception { System.out.println( " .. InitializingBean .. " ); } @Override public void destroy() throws Exception { System.out.println( " .. DisposableBean .. " ); } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println( " .. postProcessBeforeInitialization .. " ); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println( " .. postProcessAfterInitialization .. " ); return bean; } @PostConstruct public void postConstructTest() { System.out.println( " .. @PostConstruct .. " ); } @PreDestroy public void preDestroyTest() { System.out.println( " .. @PreDestroy .. " ); } } |
运行:
1
2
3
4
5
6
7
8
9
10
11
|
public class IOCTest_LifeCycle { @Test public void test01() { // 1. 创建IOC容器 AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle. class ); System.out.println( "容器创建完成" ); // 关闭容器 applicationContext.close(); } } |
执行结果:
总结:
创建:
- Bean 构造函数 ——> @PostConstruct ——> InitializingBean 接口 ——> bean 定义的 init-method——> postProcessBeforeInitialization ——> Bean 初始化完成 ——> postProcessAfterInitialization ——> 容器创建完成
销毁:
- @PreDestroy ——> DisposableBean 接口 ——> bean 定义的 destoryMethod ——> Bean销毁
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_41075649/article/details/83382763