Spring-Bean的生命周期

Posted 醉酒的小男人

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring-Bean的生命周期相关的知识,希望对你有一定的参考价值。

package lifecycle;

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

public class PersonBean implements BeanNameAware, BeanFactoryAware, InitializingBean, DisposableBean, ApplicationContextAware {
    private String name;

    public PersonBean() {
        System.out.println("========================调用PersonBean的构造器实例化==========================");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("【属性赋值】 name="+name);
        this.name = name;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("【接口ApplicationContextAware中的setApplicationContext()方法】");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("【接口BeanFactoryAware中的setBeanFactory()方法】");
    }

    @Override
    public void setBeanName(String name) {
        System.out.println("【接口BeanNameAware中的setBeanName()方法】");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("【接口DisposableBean中的destroy()方法】");
    }

    public void customDestroy() throws Exception {
        System.out.println("【自定义destroy-method】");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("【接口InitializingBean中的afterPropertiesSet()方法】初始化阶段");
    }

    public void init() {
        System.out.println("【自定义init-method】");
    }
}
package lifecycle;
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 {
        if(bean instanceof PersonBean){
            System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof PersonBean){
            System.out.println("MyBeanPostProcessor.postProcessAfterInitialization");
        }
        return bean;
    }
}
package lifecycle;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class LifeCycleTest {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean-lifecycle.xml");
        PersonBean personBean = (PersonBean) applicationContext.getBean("personBean");
        ((ClassPathXmlApplicationContext) applicationContext).destroy();
    }

}
<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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-2.5.xsd"
       default-autowire="byName">

    <bean id="personBean" class="lifecycle.PersonBean" init-method="init" destroy-method="customDestroy">
        <property name="name" value="hao are you" />
    </bean>

    <bean id="myBeanPostProcessor" class="lifecycle.MyBeanPostProcessor" />
</beans>

实例化->属性赋值->各种Aware接口的依赖(ApplicationContextAware/BeanNameAware/BeanFactoryAware)->执行BeanPostProcessor前置处理器(postProcessBeforeInitialization)->实现InitializingBean(执行AfterPropertiesSet初始化)/自定义init-method->BeanPostProcessor后置处理器(postProcessAfterInitialization)->实现DisposableBean/自定义destroy-method 

以上是关于Spring-Bean的生命周期的主要内容,如果未能解决你的问题,请参考以下文章

spring-bean 生命周期

spring-bean 生命周期

spring-bean的生命周期

引点科技私房菜专栏之Spring-Bean生命周期和作用域以及实现方式

spring-bean的生命周期

关于片段生命周期