08 Bean的生命周期

1. IOC容器中Bean的生命周期方法

SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean的生命周期的特定点执行定制的任务

1.1 SpringIOC容器对Bean的生命周期进行管理的过程

  • 通过构造器或工厂方法创建Bean实例
  • 为Bean的属性设置值和对其他Bean的引用
  • 调用Bean的初始化方法
  • Bean可以使用了
  • 当容器关闭时,调用Bean的销毁方法

在Bean的声明里设置init-methoddestroy-method属性,为Bean指定初始化和销毁方法

通过实例来说明生命周期:

Car.java

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
package cycle;/*
* @Author Sheng WenZeng
* @Date 2019/8/11 17:53
* @Version 1.0
*/

/**
* @author Sheng Wenzeng
* @ClassName Car
* @Description TODO
* @Date 2019/8/11 17:53
* @Version 1.0
*/
public class Car {
private String brand;

public Car() {
System.out.println("Car's constructor..");
}

@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
'}';
}

public void setBrand(String brand) {
System.out.println("setBrand...");
this.brand = brand;
}


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


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

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package cycle;/*
* @Author Sheng WenZeng
* @Date 2019/8/11 17:57
* @Version 1.0
*/

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author Sheng Wenzeng
* @ClassName Main
* @Description TODO
* @Date 2019/8/11 17:57
* @Version 1.0
*/
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("cycle/cycle.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car);
applicationContext.close();

}
}

cycle.xml

1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="car" class="cycle.Car" init-method="init" destroy-method="destroy" p:brand="Audi"/>
</beans>

运行结果为:

1
2
3
4
5
Car's constructor..
setBrand...
init...
Car{brand='Audi'}
destroy...

2. 创建Bean后置处理器

Bean后置处理器允许在调用初始化方法前后对Bean进行额外的处理

Bean后置处理器对IOC容器中的所有Bean实例逐一处理,而非单一实例.其典型应用是:检查Bean属性的正确性或根据特定的标准更改Bean的属性

对Bean后置处理器而言,需要实现(org.springframework.bean.factory.comfig)

BeanPostProcessor接口,在初始化方法被调用前后,Spring将把每个Bean实例分别传递给上述接口的以上两个方法:

  • 通过构造器或工厂方法创建Bean实例
  • 为Bean的属性设置值和对其他Bean的引用
  • 调用Bean的初始化方法
  • Bean可以使用了
  • 当容器关闭时,调用Bean的销毁方法

通过实例来说明Bean后置处理器

Car.java

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
package cycle;/*
* @Author Sheng WenZeng
* @Date 2019/8/11 17:53
* @Version 1.0
*/

/**
* @author Sheng Wenzeng
* @ClassName Car
* @Description TODO
* @Date 2019/8/11 17:53
* @Version 1.0
*/
public class Car {
private String brand;

public Car() {
System.out.println("Car's constructor..");
}

@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
'}';
}

public void setBrand(String brand) {
System.out.println("setBrand...");
this.brand = brand;
}


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


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

PosrProcesser.java

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
package cycle;/*
* @Author Sheng WenZeng
* @Date 2019/8/11 20:58
* @Version 1.0
*/

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
* @author Sheng Wenzeng
* @ClassName PostProcessor
* @Description TODO
* @Date 2019/8/11 20:58
* @Version 1.0
*/
public class PostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization\t" + bean + "\t" + beanName);
return bean;
}

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

Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package cycle;/*
* @Author Sheng WenZeng
* @Date 2019/8/11 17:57
* @Version 1.0
*/

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @author Sheng Wenzeng
* @ClassName Main
* @Description TODO
* @Date 2019/8/11 17:57
* @Version 1.0
*/
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("cycle/cycle.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car);
applicationContext.close();

}
}

cycle.xml

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

<bean id="car" class="cycle.Car" init-method="init" destroy-method="destroy" p:brand="Audi"/>

<bean class="cycle.PostProcessor"/>
</beans>

运行结果为:

1
2
3
4
5
6
7
Car's constructor..
setBrand...
postProcessBeforeInitialization Car{brand='Audi'} car
init...
postProcessBeforeInitialization Car{brand='Audi'} car
Car{brand='Audi'}
destroy...

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×