1. IOC容器中Bean的生命周期方法
Spring IOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的服务
具体管理的过程如下:
- 通过构造器或工厂方法创建Bean实例
- 为Bean的属性设置值和对其它Bean的引用
- 调用Bean的初始化方法
- 开始使用Bean
- 当容器关闭时,调用Bean的销毁方法
在Bean的声明里设置init-method和destory-method属性为Bean指定初始化和销毁方法
示例:
新建Car类:
public class Car {
public Car() {
System.out.println("Car's constructor...");
}
private String brand;
public void setBrand(String brand) {
System.out.println("setBrand...");
this.brand = brand;
}
public void init() {
System.out.println("init...");
}
public void destory() {
System.out.println("destory...");
}
}
配置xml文档
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 使用init-method和destory-method来指定初始化和销毁方法 -->
<bean id = "car" class = "com.spring.beans.cycle.Car" init-method="init" destroy-method="destory">
<property name="brand" value = "Audi "></property>
</bean>
</beans>
Main:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cycle.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
//关闭容器:
ctx.close();
}
}
运行结果如下图:
2. 创建Bean后置处理器
Bean后置处理器允许在调用初始化方法前后对Bean进行额外的处理。
Bean后置处理器对IOC容器里的所有Bean实例逐一处理,而非单一实例。其典型应用是检查Bean属性的正确性或根据特定的标准更改Bean的属性。
加了Bean后置处理器后Bean的生命周期管理过程如下:
- 通过构造器或工厂方法创建Bean实例
- 为Bean的属性设置值和对其它Bean的引用
- 将Bean实例传递给Bean后置处理器的postProcessBeforeInitialization方法
- 调用Bean的初始化方法
- 将Bean实例传递给Bean后置处理器的postProcessAfterInitialization方法
- 开始使用Bean
- 当容器关闭时,调用Bean的销毁方法
实例:
在上面的代码基础上新建一个MyBeanPostProcessor类:
package com.spring.beans.cycle;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization" + arg1 + "," + arg1);
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization" + arg1 + "," + arg1);
return arg0;
}
}
xml文件添加点内容修改如下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 使用init-method和destory-method来指定初始化和销毁方法 -->
<bean id = "car" class = "com.spring.beans.cycle.Car" init-method="init" destroy-method="destory">
<property name="brand" value = "Audi "></property>
</bean>
<!-- 配置bean的后置处理器 -->
<bean class = "com.spring.beans.cycle.MyBeanPostProcessor"></bean>
</beans>
允许Main得到结果如下:
2.3 在postProcessBeforeInitialization里修改点内容
MyBeanPostProcessor类修改如下:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization" + arg0 + "," + arg1);
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization" + arg0 + "," + arg1);
Car car = new Car();
car.setBrand("Ford" );
return car;
}
}
Car类里面添加一个toString()方法然后运行得到以下结果:
从图中我们可以看到在postProcessAfterInitialization那里car的brand还是Audi,init之后就变成了Ford
具体xml的配置和注释如下:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 使用init-method和destory-method来指定初始化和销毁方法 -->
<bean id = "car" class = "com.spring.beans.cycle.Car" init-method="init" destroy-method="destory">
<property name="brand" value = "Audi "></property>
</bean>
<!-- 实现BeanPostProcessor接口,并具体提供:
Object postProcessBeforeInitialization(Object arg0, String arg1);init-method之前被调用
Object postProcessAfterInitialization(Object arg0, String arg1);init-method之后被调用
的实现
arg0:bean实例本身
arg1:IOC容器中配置的Bean的名字
返回值:实际上返回给用户的Bean,注意:可以在以上两个方法中修改返回的Bean,甚至返回一个新的bean
-->
<!-- 配置bean的后置处理器 不配置id, IOC容器自动识别是一个BeanPostProcessor-->
<bean class = "com.spring.beans.cycle.MyBeanPostProcessor"></bean>
</beans>