(1)Spring Bean的生命周期介绍
Bean的生命周期简单分为4个阶段:实例化、属性赋值、初始化和销毁。实例化和属性赋值对应构造方法和setter方法的注入。
Spring之所以强大的原因是易扩展,生命周期相关的常用扩展点非常多。扩展点分2类:
1. 作用于多个Bean的增强扩展
InstantiationAwareBeanPostProcessor 作用于实例化阶段前后
BeanPostProcessor 作用于初始化阶段前后
2. 作用于单个bean的增强扩展
初始化阶段:Aware接口(BeanNameAware BeanClassLoaderAware BeanFactoryAware)
(2)Spring Bean的生命周期流程图
(3)代码演示
自定义一个InstantiationAwareBeanPostProcessor
package com.learn.blog.demo;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
public class MyInstantiationAwareBeanPostProcessor implements InstantiationAwareBeanPostProcessor {
/**
* 在目标对象实例化之前调用,该方法的返回值类型是Object,我们可以返回任何类型的值。
* 由于这个时候目标对象还未实例化,所以这个返回值可以用来代替原本该生成的目标对象的实例(比如代理对象)。
* 如果该方法的返回值代替原本该生成的目标对象,后续只有postProcessAfterInitialization方法会调用,其它方法不再调用;否则按照正常的流程走
*/
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
if ("demo6".equals(beanName)) {
System.out.println("执行InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法");
}
return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
}
/**
* 在目标对象实例化之后调用,但是该实例的属性还未被设置。
* 它的返回值决定要不要调用postProcessProperties方法的其中一个因素(另一个因素是mbd.getDependencyCheck());
* 如果该方法返回false,并且不需要check,那么postProcessProperties就会被忽略不执行;
* 如果返回true,postProcessProperties就会被执行
*/
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
if ("demo6".equals(beanName)) {
System.out.println("执行InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation方法");
}
return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
}
/**
* 对属性值进行修改,这个时候属性值还未赋值给目标对象
*/
@Override
public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
if ("demo6".equals(beanName)) {
System.out.println("执行InstantiationAwareBeanPostProcessor的postProcessProperties方法");
}
return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
}
}
自定义一个BeanPostProcessor
package com.learn.blog.demo;
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 {
if ("demo6".equals(beanName)) {
System.out.println("执行BeanPostProcessor的postProcessBeforeInitialization方法");
}
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if ("demo6".equals(beanName)) {
System.out.println("执行BeanPostProcessor的postProcessAfterInitialization方法");
}
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}
自定义一个bean
package com.learn.blog.demo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
// 实现BeanNameAware, BeanFactoryAware接口
public class Demo6 implements BeanNameAware, BeanFactoryAware {
private String name;
public Demo6() {
System.out.println("执行构造函数实例化");
}
// 重写BeanNameAware的方法
@Override
public void setBeanName(String s) {
System.out.println("执行BeanNameAware的setBeanName方法");
}
// 重写BeanFactoryAware的方法
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("执行BeanFactoryAware的setBeanFactory方法");
}
// init-method
private void init() {
System.out.println("初始化方法");
}
// destory-method
private void destroy() {
System.out.println("销毁方法");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("注入属性name");
this.name = name;
}
}
spring的配置文件BeanLifecycle.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="demo6" class="com.learn.blog.demo.Demo6"
init-method="init" destroy-method="destroy">
<property name="name" value="文大奇Quiin"></property>
</bean>
<bean id="myBeanPostProcessor" class="com.learn.blog.demo.MyBeanPostProcessor">
</bean>
<bean id="myInstantiationAwareBeanPostProcessor" class="com.learn.blog.demo.MyInstantiationAwareBeanPostProcessor">
</bean>
</beans>
启动类
public class BlogDemoApplication {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("BeanLifecycle.xml");
applicationContext.getBean("demo6");
((ClassPathXmlApplicationContext) applicationContext).close();
}
}
执行结果
执行InstantiationAwareBeanPostProcessor的postProcessBeforeInstantiation方法
执行构造函数实例化
执行InstantiationAwareBeanPostProcessor的postProcessAfterInstantiation方法
执行InstantiationAwareBeanPostProcessor的postProcessProperties方法
注入属性name
执行BeanNameAware的setBeanName方法
执行BeanFactoryAware的setBeanFactory方法
执行BeanPostProcessor的postProcessBeforeInitialization方法
初始化方法
执行BeanPostProcessor的postProcessAfterInitialization方法
销毁方法