SpringBean的生命周期

来源:互联网 发布:大数据时代pdf百度云 编辑:程序博客网 时间:2024/05/16 18:44

Spring的Bean的生命周期

  • 调用Bean的无参构造函数,初始化bean对象
  • 调用set方法,对bean的属性注入(需要注意的是,如果是采用的constructor-arg的形式注入的参数,那么第一步与第二步就是直接调用带参数的构造函数,而不是先调用无参的构造函数,再去调用set方法)
  • 如果Bean实现了BeanNameAware,那么调用setBeanName方法,设置Bean的id为BeanName
  • 如果Bean实现了ApplicationContextAware接口,那么调用setApplicationContext,设置applicationContext,方便之后根据Bean获取其applicationContext上下文
  • 如果有类实现了BeanPostProcessor接口,那么调用它的postProcessBeforeInitialization方法
  • 调用init-method指定的方法
  • 调用BeanPostProcessor接口中的postProcessAfterInitialization方法,对类的对象进行增强处理
  • 执行业务逻辑
  • 调用destory-method的方法
public class BookService implements BeanNameAware,ApplicationContextAware {    private String name;    public BookService(){        System.out.println("第一步:初始化BookService");    }    public int add(){        System.out.println("增加了一本图书:"+name);        return 1;    }    public void setBeanName(String name) {        System.out.println("第三步:实现了BeanNameAware接口,调用setBeanName方法,设置BeanName为id值");    }    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {        System.out.println("第四步:实现了ApplicationContextAware接口,调用setApplicationContext方法,设置applicationContext");    }    public void init(){        System.out.println("第六步:调用init-method指定的方法");    }    public void destory(){        System.out.println("第七步:调用destory-method指定的方法");    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;        System.out.println("第二步:对BookService进行初始化name属性");    }}
public class MyBeanPostProcessor implements BeanPostProcessor {    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {        System.out.println("第五步:调用postProcessBeforeInitialization");        return bean;    }    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {        System.out.println("第七步:调用postProcessAfterInitialization");        return bean;    }}
<bean id="bookService" name="bookService" class="com.njust.learning.spring.service.BookService"      init-method="init" destroy-method="destory">    <property name="name" value="spring实战"></property></bean><bean class="com.njust.learning.spring.service.MyBeanPostProcessor"></bean>
public class TestBookService {    @Test    public void testBookService(){        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");        BookService bookService = context.getBean(BookService.class);        bookService.add();        context.close();    }}

运行的结果是:

**第一步:初始化BookService**五月 14, 2017 8:43:30 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@614c97d4: defining beans [bookService,com.njust.learning.spring.service.MyBeanPostProcessor#0]; root of factory hierarchy***第二步:对BookService进行初始化name属性****第三步:实现了BeanNameAware接口,调用setBeanName方法,设置BeanName为id值****第四步:实现了ApplicationContextAware接口,调用setApplicationContext方法,设置applicationContext****第五步:调用postProcessBeforeInitialization****第六步:调用init-method指定的方法****第七步:调用postProcessBeforeInitialization**增加了一本图书:spring实战五月 14, 2017 8:43:30 下午 org.springframework.context.support.AbstractApplicationContext doClose信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@154d5994: startup date [Sun May 14 20:43:29 CST 2017]; root of context hierarchy五月 14, 2017 8:43:30 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@614c97d4: defining beans [bookService,com.njust.learning.spring.service.MyBeanPostProcessor#0]; root of factory hierarchy**第七步:调用destory-method指定的方法**

注意
1. init-method也可以换成InitializingBean接口,实现其中的afterProperties方法
2. destory-method也可以换成DisposableBean接口的destory方法
3. ApplicationContextAware也可以换成BeanFactoryAware,调用setBeanFactory方法,返回BeanFactory实例,当然也可以两个都保留,那样的话,会先调用setBeanFactory方法,在调用setApplicationContext方法,因为applicationContext是继承自BeanFactory方法的

  • 使用BeanFactory与使用ApplicationContexy获取bean的异同
    BeanFactory在使用到这个类的时候才会加载这个类,ApplicationContext则是在加载配置文件的时候创建所有的类
  • ApplicationContext还提供了相应的扩展
0 0
原创粉丝点击