1. 生命中周期
SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制任务。
- SpringIOC容器对Bean的生命周期进行管理的过程。
- 通过构造器或工厂方法创建Bean实例。
- 为Bean的属性设置值和对其他Bean的引用。
- 调用Bean的初始化方法。
- Bean可以使用了。
- 当容器关闭时,调用Bean的销毁方法。
- 在Bean的声明里设置init-method和destory-method属性,为Bean指定初始化和销毁方法。
2. 示例如下:
2.1 正常周期
这里定义了一个Car类:
public class Car {
public Car() {
System.out.println("Constructor init....");
}
private String brand;
public void setBrand(String brad) {
System.out.println("setBrand");
this.brand=brand;
}
public void init() {
System.out.println("init...");
}
public void destroy() {
System.out.println("destory...");
}
}
beans-cycle.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">
<!--定义初始化和销毁方法-->
<bean id="car" class="com.atguigu.spring.cycle.Car" init-method="init"
destroy-method="destroy"
>
<property name="brand" value="Audi"></property>
</bean>
</beans>
Main类来调用上述类和配置文件,Main:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctxApplicationContext=new ClassPathXmlApplicationContext("beans-cycle.xml");
Car car=(Car) ctxApplicationContext.getBean("car");
System.out.println(car);
ctxApplicationContext.close();
}
}
输出如下:
2月 03, 2020 6:16:15 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1786dec2: startup date [Mon Feb 03 18:16:15 CST 2020]; root of context hierarchy
2月 03, 2020 6:16:16 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-cycle.xml]
Constructor init....
setBrand
init...
com.atguigu.spring.cycle.Car@6fd83fc1
2月 03, 2020 6:16:16 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1786dec2: startup date [Mon Feb 03 18:16:15 CST 2020]; root of context hierarchy
destory..
2.1 加入后置处理的周期
在对bean进行初始化操作之前,对bean进行额外的处理。
Bean后置处理器对IOC容器里所有的Bean示例逐一处理,而非单一实例,其典型操作是:检查Bean属性的正确性或根据特定的标准更改Bean的属性。
对Bean处理器而言,需要实现 interface BeanPostProcessor接口,在初始化方法被调用前后,Spring将把每个Bean实例分别传递给上述接口的以下两个方法。
- postProcessBeforeInitialization(Object bean, String beanName)
- postProcessAfterInitialization(Object bean, String beanName)
示例如下:
MyBeanPostProcessor类:
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization: "+bean+", "+beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization: "+bean+", "+beanName);
return bean;
}
}
配置Beans-cycle:
<?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">
<bean id="car" class="com.atguigu.spring.cycle.Car" init-method="init"
destroy-method="destroy"
>
<property name="brand" value="Audi"></property>
</bean>
<!-- 配置bean的后置处理器 -->
<bean class="com.atguigu.spring.cycle.MyBeanPostProcessor" >
</bean>
</beans>
Main类如上一小节。
输出结果如下:
2月 03, 2020 6:42:17 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1786dec2: startup date [Mon Feb 03 18:42:17 CST 2020]; root of context hierarchy
2月 03, 2020 6:42:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-cycle.xml]
Constructor init....
setBrand
postProcessBeforeInitialization: com.atguigu.spring.cycle.Car@5d7148e2, car
init...
postProcessAfterInitialization: com.atguigu.spring.cycle.Car@5d7148e2, car
com.atguigu.spring.cycle.Car@5d7148e2
2月 03, 2020 6:42:18 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1786dec2: startup date [Mon Feb 03 18:42:17 CST 2020]; root of context hierarchy
destory...