bean的生命周期

bean的生命周期

BV1L14y1S7cf的笔记

步骤

五步骤版本

  1. 实例化:创建对象
  2. 依赖注入:给成员变量赋值
  3. 初始化:执行自定义的初始化方法
  4. 使用
  5. 销毁

例子

Dog类,有成员变量name,自定义的初始化和销毁方法

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
public class Dog {

private String name;

public Dog() {
System.out.println("1. 实例化");
}

public Dog(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
System.out.println("2. 依赖注入");
this.name = name;
}

public void myInit() {
System.out.println("3. 初始化");
}

public void myDestroy() {
System.out.println("5. 销毁bean");
}
}

通过配置文件将Dog类交给Spring管理,并指定初始化和销毁方法,还有给成员变量赋值

1
2
3
4
5
6
7
8
9
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dog" class="com.xw.bean.Dog" init-method="myInit" destroy-method="myDestroy">
<property name="name" value="旺财"></property>
</bean>

</beans>

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class MainApplication {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Dog dog = applicationContext.getBean(Dog.class);
System.out.println("4.使用bean");
applicationContext.close();
}
}
// 打印
/*
1. 实例化
2. 依赖注入
3. 初始化
4.使用bean
5. 销毁bean
旺财
*/

七步骤版本

  1. 实例化:创建对象

  2. 依赖注入:给成员变量赋值

    初始化前执行 BeanPostProcessor before方法

  3. 初始化:执行自定义的初始化方法

    初始化前执行 BeanPostProcessor after方法

  4. 使用

  5. 销毁

例子

创建自定义的Bean后置处理器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Dog && "dog".equals(beanName))
System.out.println(" 执行后置处理器的before方法");
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Dog && "dog".equals(beanName))
System.out.println(" 执行后置处理器的after方法");
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}

交给Spring管理

1
2
3
4
5
6
7
8
9
10
11
12
13
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="dog" class="com.xw.bean.Dog" init-method="myInit" destroy-method="myDestroy">
<property name="name" value="旺财"></property>
</bean>

<bean id="myBeanPostProcessor" class="com.xw.bean.MyBeanPostProcessor">

</bean>

</beans>

使用

1
2
3
4
5
6
7
8
9
10
// 打印
/*
1. 实例化
2. 依赖注入
执行后置处理器的before方法
3. 初始化
执行后置处理器的after方法
4.使用bean
5. 销毁bean
*/

十步骤版本

  1. 实例化:创建对象

  2. 依赖注入:给成员变量赋值

    执行回调函数…Aware

    初始化前执行 BeanPostProcessor before方法

    执行正在初始化bean函数

  3. 初始化:执行自定义的初始化方法

    初始化前执行 BeanPostProcessor after方法

  4. 使用

    执行销毁bean方法

  5. 销毁

例子

实现BeanNameAware, InitializingBean, DisposableBean接口

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 Dog implements BeanNameAware, InitializingBean, DisposableBean {

private String name;

public Dog() {
System.out.println("1. 实例化");
}

public Dog(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
System.out.println("2. 依赖注入");
this.name = name;
}

public void myInit() {
System.out.println("3. 初始化");
}

public void myDestroy() {
System.out.println("5. 销毁bean");
}

@Override
public void setBeanName(String name) {
System.out.println(" 执行回调函数");
}

@Override
public void afterPropertiesSet() throws Exception {
System.out.println(" 执行正在初始化bean函数");
}

@Override
public void destroy() throws Exception {
System.out.println(" 执行销毁bean方法");
}
}

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 打印
/*
1. 实例化
2. 依赖注入
执行回调函数
执行后置处理器的before方法
执行正在初始化bean函数
3. 初始化
执行后置处理器的after方法
4.使用bean
执行销毁bean方法
5. 销毁bean

进程已结束,退出代码为 0

*/

InitializingBean作用

InitializingBean的afterPropertiesSet()是在依赖注入完、执行完回调、执行完后处理器的before后调用的。这个时候成员变量已经实例化完,可以通过afterPropertiesSet()方法来给变量进行一些初始化操作

如:

  1. 数据库连接池的初始化
    在连接池的初始化过程中,可以通过实现InitializingBean接口,在Bean初始化完成后自动调用afterPropertiesSet()方法,进行连接池的初始化工作。
  2. 配置文件加载
    在Spring中,可以通过实现InitializingBean接口,使用afterPropertiesSet()方法,在Bean属性设置完成后,进行配置文件的加载工作,从而减少了代码量和配置文件的加载时间。
  3. 线程池初始化
    在Java中创建线程池时,可以通过实现InitializingBean接口,使用afterPropertiesSet()方法,在Bean属性设置完成后,进行线程池的初始化工作。
  4. 数据初始化
    在某些场景下,需要在Bean初始化完成后,进行数据初始化的操作。可以通过实现InitializingBean接口,在Bean属性设置完成后,使用afterPropertiesSet()方法,进行相关数据的初始化工作。

BeanPostProcessor作用

BeanPostProcessor是Spring IoC容器提供的一个扩展接口,主要作用是在Spring IoC容器实例化Bean对象后,对其进行加工和装饰,添加功能

如:

  1. AOP代理
    通过before或after方法,在Bean对象装配之后,对Bean对象进行AOP代理,从而在不修改原有代码的情况下,实现对Bean对象的增强或切面操作,如事务管理、权限控制、日志记录、性能监控等。

bean的生命周期
http://xwww12.github.io/2023/06/13/spring/bean的生命周期/
作者
xw
发布于
2023年6月13日
许可协议