Bean的生命周期

  1. Spring启动,查找并加载需要被Spring管理的bean,进行Bean的实例化.
  2.  Bean实例化后对将Bean的引入和值注入到Bean的属性中
  3. Bean实例化后对将Bean的引入和值注入到Bean的属性中
  4. 如果Bean实现了BeanFactoryAware接口的话,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传入
  5. 如果Bean实现了ApplicationContextAware接口的话,Spring将调用Bean的setApplicationContext()方法,将bean所在应     用上下文引用传入进来。
  6. 如果Bean实现了BeanPostProcessor接口,Spring就将调用他们的postProcessBeforeInitialization()方法。
  7. 如果Bean 实现了InitializingBean接口,Spring将调用他们的afterPropertiesSet()方法。类似的,如果bean使用init-method声明了初始化方法,该方法也会被调用
  8. 如果Bean 实现了BeanPostProcessor接口,Spring就将调用他们的postProcessAfterInitialization()方法。
  9. 此时,Bean已经准备就绪,可以被应用程序使用了。他们将一直驻留在应用上下文中,直到应用上下文被销毁。
  10. 如果bean实现了DisposableBean接口,Spring将调用它的destory()接口方法,同样,如果bean使用了destory-method 声明销毁方法,该方法也会被调用。

 

Bean的生命周期验证:

Book.class

package com.demo.cn.springbeanlifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

public class Book implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {

    String bookname;

    public Book() {
        System.out.println("Book construct initialing...");
    }

    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("Book.setFactory invoke");

    }

    public void setBeanName(String name) {
        System.out.println("Book.setBeanname invole..");

    }

    public void destroy() throws Exception {
        System.out.println("Book.destory invoke...");

    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("afterproperty方法被执行。。");

    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("book.setapplication invoke");
    }


    public String getBookname() {
        return bookname;
    }

    public void setBookname(String bookname) {
        this.bookname = bookname;
        System.out.println("setBookname : book name has set");
    }



    //@PostConstruct
    public void springPostContruct() {
        System.out.println("init-method方法被执行。。。。");
    }

    public void myPreDestory() {
        System.out.println("Book.myPreDestory invoke");
        System.out.println("---------------destroy-----------------");
    }

    //自定义销毁方法
    @PreDestroy
    public void springPreDestory(){
        System.out.println("@PreDestory");
    }


    @Override
    protected void finalize() throws Throwable {
        System.out.println("------inside finalize-----");
    }
}

 

自定义实现BeanPostProcessor的MyBeanPostProcessor

package com.demo.cn.springbeanlifecycle;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class MyBeanPostProcessor implements BeanPostProcessor {
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Book) {
            System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization");
        }
        return bean;
    }

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof  Book) {
            System.out.println("MyBeanPostProcessor.postProcessAfterInitialization");
        }
        return bean;
    }
}

 在resources 目录下新建Bean-Lifecycle.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan base-package="com.demo.cn.springbeanlifecycle"/>

    <bean id="book" class="com.demo.cn.springbeanlifecycle.Book" init-method="springPostContruct" destroy-method="myPreDestory">
        <property name="bookname" value="think in java"/>
    </bean>

    <bean class="com.demo.cn.springbeanlifecycle.MyBeanPostProcessor"/>



</beans>

做一个启动类的测试

package com.demo.cn.springbeanlifecycle;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class springBeanLifeCycleTest {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Bean-Lifecycle.xml");
        Book book = (Book) applicationContext.getBean("book");
        System.out.println("Book name = " + book.getBookname());
        ((ClassPathXmlApplicationContext) applicationContext).destroy();
    }
}

查看控制台打印:

即可验证Spring bean 的生命周期。