Spring Bean 生命周期
一、spring的bean创建和销毁
1、spring只帮我们管理单例bean。
2、spring容器默认是使用单例模式,容器启动时创建所有单例bean,容器关闭时销毁bean
3、对于 prototype 的 bean 多例模式,何时调用getBean对象时创建bean对象,之后则不会再管理后续的生命周期(不会调用destory方法)。
二、注意点
1、BeanPostProcessor 后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行
三、生命周期图
Person 类型
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Person implements BeanNameAware,BeanFactoryAware,
InitializingBean, DisposableBean, ApplicationContextAware {
private String name;
private String address;
private String phone;
private BeanFactory beanFactory;
private String beanName;
public Person() {
System.out.println("【构造器】调用Person的构造器实例化=======>>>");
}
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("【注入属性】注入属性name");
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
System.out.println("【注入属性】注入属性address");
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
System.out.println("【注入属性】注入属性phone");
this.phone = phone;
}
@Override
public String toString() {
return "Person [address=" + address + ", name=" + name + ", phone="
+ phone + "]";
}
// 这是BeanNameAware接口方法
@Override
public void setBeanName(String arg0) {
System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
this.beanName = arg0;
}
// 这是BeanFactoryAware接口方法
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out
.println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
this.beanFactory = arg0;
}
// 这是InitializingBean接口方法
@Override
public void afterPropertiesSet() throws Exception {
System.out
.println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
}
// 这是 DisposableBean 接口方法
@Override
public void destroy() throws Exception {
System.out.println("【DisposableBean接口】DisposableBean.destroy()");
}
// 通过<bean>的init-method属性指定的初始化方法
public void myInit() {
System.out.println("【init-method】调用<bean>的init-method属性指定的初始化方法");
}
// 通过<bean>的destroy-method属性指定的初始化方法
public void myDestroy() {
System.out.println("【destroy-method】调用<bean>的destroy-method属性指定的初始化方法");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("【ApplicationContext 接口】调用ApplicationContext.setApplicationContext()");
}
}
MyBeanPostProcessor 后置处理器
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class MyBeanPostProcessor implements BeanPostProcessor {
public MyBeanPostProcessor() {
super();
System.out.println("这是 BeanPostProcessor 实现类构造器!!=======>>>");
// TODO Auto-generated constructor stub
}
@Override
public Object postProcessAfterInitialization(Object arg0, String arg1)
throws BeansException {
System.out
.println("【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessAfterInitialization()方法 对属性进行更改!");
return arg0;
}
@Override
public Object postProcessBeforeInitialization(Object arg0, String arg1)
throws BeansException {
System.out
.println("【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessBeforeInitialization()方法 对属性进行更改!");
return arg0;
}
}
partBeanLife/beans.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="beanPostProcessor" class="partBeanLife.MyBeanPostProcessor">
</bean>
<bean id="person" class="partBeanLife.Person" init-method="myInit" destroy-method="myDestroy"
p:name="张三" p:address="广州" p:phone="18979696249" />
</beans>
测试类:
public class BeanLifeCycle {
public static void main(String[] args) {
System.out.println("现在开始初始化容器");
ApplicationContext factory = new ClassPathXmlApplicationContext("partBeanLife/beans.xml");
System.out.println("容器初始化成功");
//得到Person,并使用
Person person = factory.getBean("person",Person.class);
System.out.println(person);
System.out.println("现在开始关闭容器!");
((ClassPathXmlApplicationContext)factory).registerShutdownHook();
}
}
输出结果:
现在开始初始化容器
这是 BeanPostProcessor 实现类构造器!!=======>>>
【构造器】调用Person的构造器实例化=======>>>
【注入属性】注入属性address
【注入属性】注入属性name
【注入属性】注入属性phone
【BeanNameAware接口】调用BeanNameAware.setBeanName()
【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()
【ApplicationContext 接口】调用ApplicationContext.setApplicationContext()
【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessBeforeInitialization()方法 对属性进行更改!
【InitializingBean接口】调用InitializingBean.afterPropertiesSet()
【init-method】调用<bean>的init-method属性指定的初始化方法
【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessAfterInitialization()方法 对属性进行更改!
容器初始化成功
Person [address=广州, name=张三, phone=18979696249]
现在开始关闭容器!
【DisposableBean接口】DisposableBean.destroy()
【destroy-method】调用<bean>的destroy-method属性指定的初始化方法
[Spring Bean的生命周期(非常详细)]:https://www.cnblogs.com/zrtqsk/p/3735273.html