美文网首页
Spring(三)--Bean生命周期

Spring(三)--Bean生命周期

作者: 炳学 | 来源:发表于2019-09-25 09:55 被阅读0次

    基本概念

    在Spring中,可以从两个层面定义Bean的生命周期,第一个层面是Bean的作用范围;第二个层面是实例化Bean时所经历的一系列阶段。

    BeanFactory中Bean的生命周期

    1.生命周期过程图解

    生命周期图解

    假设一个Bean实现了所有的接口,大的概况一下Bean的生命周期:

    1.实例化BeanFactoryPostProcessor:处理的对象是BeanFactory级别

    2.实例化BeanPostProcessor实现类

    3.实例化InstantiationAwareBeanPostProcessorAdapter实现类,注:该类是BeanPostProcessor的扩展

    4.执行InstantiationAwareBeanPostProcessorAdapter类的postProcessBeforeInstantiation方法

    5.Bean的构造方法

    6.执行InstantiationAwareBeanPostProcessorAdapter类的postProcessPropertyValues

    7.为Bean注入属性,即依赖注入

    8.调用BeanNameAware的setBeanName方法

    9.调用BeanNameAware的setBeanFactory方法

    10.执行BeanPostProcessor的后置处理器,postProcessAfterInitialization方法

    11.调用InitializingBean的afterPropertiesSet方法

    12.调用bean的init-method初始化方法

    13.执行BeanPostProcessor的postProcessAfterInitialization

    14.执行InstantiationAwareBeanPostProcessorAdapter的后置方法,postProcessAfterInitialization

    15.Bean的使用

    16.调用DiposibleBean的destory方法

    17.调用bean指定的destory-method方法

    用代码验证测试

    测试Bean - StudentBean类:

    /**
     * 测试生命周期的Bean
     */
    public class StudentBean implements InitializingBean, DisposableBean, BeanNameAware, BeanFactoryAware {
        private String name;
        private int age;
     
        private String beanName;         //实现了BeanNameAware接口,Spring可以将BeanName注入该属性中
        private BeanFactory beanFactory; //实现了BeanFactory接口,Spring可将BeanFactory注入该属性中
     
     
        public StudentBean(){
            System.out.println("【Bean构造方法】学生类的无参构造方法");
        }
     
        @Override
        public String toString() {
            return "StudentBean{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", beanName='" + beanName + '\'' +
                    '}';
        }
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            System.out.println("【set注入】注入学生的name属性");
            this.name = name;
        }
     
        public int getAge() {
            return age;
        }
     
        public void setAge(int age) {
            System.out.println("【set注入】注入学生的age属性");
            this.age = age;
        }
     
        /**
         * 自己编写的初始化方法
         */
        public void myInit(){
            System.out.println("【init-method】调用init-method属性配置的初始化方法");
        }
     
        /**
         * 自己编写的销毁方法
         */
        public void myDestroy(){
            System.out.println("【destroy-method】调用destroy-method属性配置的销毁方法");
        }
     
        /**
         * BeanFactoryAware接口的方法
         * @param beanFactory
         * @throws BeansException
         */
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            this.beanFactory = beanFactory;
            System.out.println("【BeanFactoryAware接口】调用BeanFactoryAware的setBeanFactory方法得到beanFactory引用");
        }
     
        /**
         * BeanNameAware接口的方法
         * @param name
         */
        public void setBeanName(String name) {
            this.beanName = name;
            System.out.println("【BeanNameAware接口】调用BeanNameAware的setBeanName方法得到Bean的名称");
        }
     
        /**
         * InitializingBean接口的方法
         * @throws Exception
         */
        public void afterPropertiesSet() throws Exception {
            System.out.println("【InitializingBean接口】调用InitializingBean接口的afterPropertiesSet方法");
        }
     
        /**
         * DisposableBean接口的方法
         * @throws Exception
         */
        public void destroy() throws Exception {
            System.out.println("【DisposableBean接口】调用DisposableBean接口的destroy方法");
        }
    }
    

    MyBeanFactoryPostProcessor工厂处理器类:

    public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
     
        public MyBeanFactoryPostProcessor() {
            System.out.println("【BeanFactoryPostProcessor接口】调用BeanFactoryPostProcessor实现类构造方法");
        }
     
        /**
         * 重写BeanFactoryPostProcessor接口的postProcessBeanFactory方法,可通过该方法对beanFactory进行设置
         */
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
                throws BeansException {
            System.out.println("【BeanFactoryPostProcessor接口】调用BeanFactoryPostProcessor接口的postProcessBeanFactory方法");
        }
    }
    

    MyInstantiationAwareBeanPostProcessor类:

    /**
     * 一般情况下,当我们需要实现InstantiationAwareBeanPostProcessor接口时,是通过继承Spring框架中InstantiationAwareBeanPostProcessor接口实现类
     * InstantiationAwareBeanPostProcessorAdapter这个适配器类来简化我们实现接口的工作
     */
    public class MyInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
     
        public MyInstantiationAwareBeanPostProcessor() {
            System.out.println("【InstantiationAwareBeanPostProcessor接口】调用InstantiationAwareBeanPostProcessor构造方法");
        }
     
        /**
         * 实例化Bean之前调用
        */
        public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException {
            System.out.println("【InstantiationAwareBeanPostProcessor接口】调用InstantiationAwareBeanPostProcessor接口的postProcessBeforeInstantiation方法");
            return null;
        }
     
        /**
         * 实例化Bean之后调用
         */
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("【InstantiationAwareBeanPostProcessor接口】调用InstantiationAwareBeanPostProcessor接口的postProcessAfterInitialization方法");
            return bean;
        }
     
        /**
         * 设置某个属性时调用
         */
        public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)
                throws BeansException {
            System.out.println("【InstantiationAwareBeanPostProcessor接口】调用InstantiationAwareBeanPostProcessor接口的postProcessPropertyValues方法");
            return pvs;
        }
    }
    

    MyBeanPostProcessor类:

    public class MyBeanPostProcessor implements BeanPostProcessor {
     
        public MyBeanPostProcessor(){
            System.out.println("【BeanPostProcessor接口】调用BeanPostProcessor的构造方法");
        }
     
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("【BeanPostProcessor接口】调用postProcessBeforeInitialization方法,这里可对"+beanName+"的属性进行更改。");
            return bean;
        }
     
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("【BeanPostProcessor接口】调用postProcessAfterInitialization方法,这里可对"+beanName+"的属性进行更改。");
            return bean;
        }
    }
    

    XML配置:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
        
        <!-- 生命周期测试  -->
        <!--配置Bean的后置处理器-->
        <bean id="beanPostProcessor" class="com.my.spring.life.MyBeanPostProcessor">
        </bean>
     
        <!--配置instantiationAwareBeanPostProcessor-->
        <bean id="instantiationAwareBeanPostProcessor" class="com.my.spring.life.MyInstantiationAwareBeanPostProcessor">
        </bean>
     
        <!--配置BeanFactory的后置处理器-->
        <bean id="beanFactoryPostProcessor" class="com.my.spring.life.MyBeanFactoryPostProcessor">
        </bean>
     
        <bean id="studentBean" class="com.my.spring.life.StudentBean" init-method="myInit"
              destroy-method="myDestroy" scope="singleton">
            <property name="name" value="yanxiao"></property>
            <property name="age" value="21"></property>
        </bean>
    </beans>
    

    测试类:

    public class TestCyclelife {
     
        /*
         */
        public static void main(String[] args){
            System.out.println("--------------【初始化容器】---------------");
     
            ApplicationContext context = new ClassPathXmlApplicationContext("Springlife.xml");
            System.out.println("-------------------【容器初始化成功】------------------");
            //得到studentBean,并显示其信息
            StudentBean studentBean = context.getBean("studentBean",StudentBean.class);
            System.out.println(studentBean);
     
            System.out.println("--------------------【销毁容器】----------------------");
            ((ClassPathXmlApplicationContext)context).registerShutdownHook();
        }
    }
    

    测试结果如下:

    结果

    转载自:https://blog.csdn.net/weixin_40834464/article/details/82829999


    相关文章

      网友评论

          本文标题:Spring(三)--Bean生命周期

          本文链接:https://www.haomeiwen.com/subject/vfbiyctx.html