Spring Bean的生命周期

来源:互联网 发布:怎么做淘客软件 编辑:程序博客网 时间:2024/06/06 02:50

  这篇文章里主要回顾Spring里Bean的生命周期,并通过一个例子来验证其生命周期。


Bean生命周期:

  1. Spring调用bean构造器,创建实例对象;
  2. Spring调用bean的setXxx()方法,将属性注入进该bean;
  3. 如果bean实现了BeanNameAware接口,则调用执行setBeanName(String beanId)方法;
  4. 如果bean实现了BeanFactoryAware接口,则调用执行setBeanFactory(BeanFactory factory)方法;
  5. 如果该bean实现了ApplicationContextAware这个接口,则Spring此时会调用执行setApplicationContext(ApplicationContext arg0)方法;
  6. 如果容器注册了BeanPostProcessor的实现类(注意这里是容器,而不是bean),则调用执行postProcessBeforeInitialization(Object bean, String beanName)方法;
  7. 如果bean实现了InitializingBean接口,Spring将调用afterPropertiesSet()方法;
  8. 如果bean在配置文件里配置了init-method属性,则执行init-method指定的方法;
  9. 如果容器注册了BeanPostProcessor的实现类(注意这里是容器,而不是bean),则调用执行postProcessorAfterInitialization(Object bean, String beanName)方法;
  10. 此时,bean的初始化准备就绪,可以被应用程序使用了,下面的例子里在Main主类里调用Service bean的业务方法service();
  11. 如果bean实现了DisposableBean接口,Spring将调用destroy()方法;
  12. 如果bean在配置文件里配置了destroy-method属性,则执行destroy-method指定的方法。

代码验证:
接口:

public interface IService {}

接口实现类:

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.beans.factory.config.BeanPostProcessor;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;public class Service implements IService, BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean{    private String service;    public Service(){        System.out.println("Constructor:构造器方法");    }    public void setService(String arg0){        this.service = arg0;        System.out.println("setXxx:setService方法");    }    @Override    public void setBeanName(String name) {        // TODO Auto-generated method stub        System.out.println("BeanNameAware:setBeanName方法");    }    @Override    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {        // TODO Auto-generated method stub        System.out.println("BeanFactoryAware:setBeanFactory方法");    }    @Override    public void setApplicationContext(ApplicationContext arg0) throws BeansException {        // TODO Auto-generated method stub    System.out.println("ApplicationContextAware:setApplicationContext方法");    }    @Override    public void afterPropertiesSet() throws Exception {        // TODO Auto-generated method stub    System.out.println("InitializingBean:afterPropertiesSet");    }    public void serviceInit(){        System.out.println("init-method:serviceInit");    }    public void service(){        System.out.println("service:业务方法");    }    @Override    public void destroy() throws Exception {        // TODO Auto-generated method stub        System.out.println("DisposableBean:destroy");    }    public void serviceDestroy(){        System.out.println("destroy-method:serviceDestroy");    }}

Bean后处理器类

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("BeanPostProcessor:bean后处理器before方法");        return bean;    }    @Override    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {        System.out.println("BeanPostProcessor:bean后处理器after方法");        return bean;    }}

配置文件:config.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-3.2.xsd">    <bean id="service" class="bean.lifecycle.Service" init-method="serviceInit" destroy-method="serviceDestroy">        <property name="service" value="service"></property>    </bean>    <bean class="bean.lifecycle.MyBeanPostProcessor"/></beans>

主类:

import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[] args){        ApplicationContext ac = new ClassPathXmlApplicationContext("config.xml");        Service service = (Service)ac.getBean("service");        service.service();        ((ClassPathXmlApplicationContext)ac).close();    }}

代码运行结果:

Constructor:构造器方法
setXxx:setService方法
BeanNameAware:setBeanName方法
BeanFactoryAware:setBeanFactory方法
ApplicationContextAware:setApplicationContext方法
BeanPostProcessor:bean后处理器before方法
InitializingBean:afterPropertiesSet
init-method:serviceInit
BeanPostProcessor:bean后处理器after方法
service:业务方法
DisposableBean:destroy
destroy-method:serviceDestroy

  这段代码有几个需要注意的地方:

  1. BeanPostProcessor必须由另一个类实现,不能由Service类实现,否则的话,容器启动时并不会执行BeanPostProcessor里的两个方法,也就是说BeanPostProcessor的实现类所影响的范围是容器里没有实现BeanPostProcessor接口的类;
  2. ApplicationContext接口里并没有定义close()方法,但是其实现类里有这个方法,用来关闭容器,容器里的bean直到该容器应用上下文销毁才会被销毁。