1、前言
说到生命周期,大家肯定不会陌生,这是学到哪里都会提到的东西。最开始学习Java时,曾经提到过各种变量的生命周期(局部变量、成员变量等),后来学习servlet也提到过它的生命周期,现在学到Spring,它创建的对象也有生命周期。
学习生命周期是是为了让我们更好的直到一个对象从创建到销毁的过程,既然这些东西都不是我们创建的,我们有义务知道它们的各个过程是怎样的,我们才能更好的使用它们。
2、Spring Bean的生命周期
Spring Bean生命周期如下:
- 首先要根据我们的要求创建容器。
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
- 然后去实例化对象。也就是通过getBean先去单例池当中找是否创建了该对象,没有的话就去创建。
- 实例化对象以后,这就是个空壳,需要对属性进行赋值。
- 完成初始化过程
这里是一个有顺序的过程,如果实现了某个接口,以及其中的步骤那么就会按照下图的顺序执行。
- 执行AOP操作
- 这样对象就变成了bean
我们知道bean和对象其实是不一样的,bean是Spring给我们创建的对象,而不是所有对象都可以叫做bean,只有Spring执行完所有的操作后这个对象才是bean。
3、代码演示
先创建一个MyTest类。其中按照上图那样逐一实现接口方法。
public class MyTest implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean {
@Autowired
public Student student;
public void setBeanName(String s) {
//setBeanName(s);
System.out.println("执行BeanNameAware接口的setBeanName方法");
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("执行BeanFactoryAware接口的setBeanFactory方法");
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("执行ApplicationContextAware接口的setApplicationContext方法");
}
public void init(){
System.out.println("执行MyTest类的init方法");
}
public void destroy(){
System.out.println("执行MyTest类的destroy销毁方法");
}
public void afterPropertiesSet() throws Exception {
System.out.println("执行InitializingBean接口的afterPropertiesSet方法");
}
}
这里面还有预初始话和初始化后方法,需要实现BeanPostProcessor类。
public class BeanPostProcessorImpl implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("执行BeanPostProcessor接口的postProcessBeforeInitialization方法");
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("执行BeanPostProcessor接口的postProcessAfterInitialization方法");
return bean;
}
}
最后在xml配置中把这几个类引入,因为Student类什么都没写,就创建了个类,就不在这里写了。
<?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="myTest" class="com.xuangong.pojo.MyTest" init-method="init" destroy-method="destroy">
</bean>
<bean id="student" class="com.xuangong.pojo.Student">
</bean>
<bean id="beanPostProcessor" class="com.xuangong.pojo.BeanPostProcessorImpl">
</bean>
</beans>
测试。
public class test {
@Test
public void test01(){
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
MyTest myTest = (MyTest) ac.getBean("myTest");
System.out.println(myTest);
}
}
结果。
执行BeanNameAware接口的setBeanName方法
执行BeanFactoryAware接口的setBeanFactory方法
执行ApplicationContextAware接口的setApplicationContext方法
执行BeanPostProcessor接口的postProcessBeforeInitialization方法
执行InitializingBean接口的afterPropertiesSet方法
执行MyTest类的init方法
执行BeanPostProcessor接口的postProcessAfterInitialization方法
执行BeanPostProcessor接口的postProcessBeforeInitialization方法
执行BeanPostProcessor接口的postProcessAfterInitialization方法
com.xuangong.pojo.MyTest@6b9651f3
执行MyTest类的destroy销毁方法