Spring Bean的生命周期

        Spring Bean的生命周期,老生常谈的面试高频问题了。这个问题即考察对Spring的微观了解,又考察对Spring的宏观认识,想要答好并不容易!

        首先粗略来看,bean的生命周期主要分为以下4个步骤

                1. 实例化 Instantiation

                2. 属性赋值 Populate

                3. 初始化 Initialization

                4. 销毁 Destruction

        其中,“实例化”阶段就对应了通过构造函数创建一个对象,“属性赋值”就是通过field属性等的赋值。

        创建一个bean时,调用了doCreateBean()方法,其中doCreateBean()方法中就是顺序调用以下三个方法,这三个方法与三个生命周期阶段一一对应。

        1.createBeanInstance() -> 实例化

        2.populateBean() -> 属性赋值

        3.initializeBean() -> 初始化

// 忽略了无关代码
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
      throws BeanCreationException {

   // Instantiate the bean.
   BeanWrapper instanceWrapper = null;
   if (instanceWrapper == null) {
       // 实例化阶段!
      instanceWrapper = createBeanInstance(beanName, mbd, args);
   }

   // Initialize the bean instance.
   Object exposedObject = bean;
   try {
       // 属性赋值阶段!
      populateBean(beanName, mbd, instanceWrapper);
       // 初始化阶段!
      exposedObject = initializeBean(beanName, exposedObject, mbd);
   }

   
   }

        至于销毁,是在容器关闭时调用的。

       

        下图是一个整体了bean的生命周期的流程图:

 1、实例化前置

        实例化前置使用的是 InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(Class<?> beanClass, String beanName) 方法,方法里有2个参数,分别是beanClass和beanName,顾名思义,就是对在对象实例化之前对bean对象的class信息进行修改或者扩展,以达到我们想要的功能,它的底层是动态代理AOP技术实现的;且是bean生命周期中最先执行的方法

2、实例化对象
        doCreateBean方法创建实例,用反射技术创建,这个没什么好说的,只是相当于new了一个对象出来而已,但需要注意的是,这个时候只是将对象实例化了,对象内的属性还未设置;

3、实例化后置

        方法名称: InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation(Object bean, String beanName)。

        在目标对象实例化之后调用,这个时候对象已经被实例化,但是该实例的属性还未被设置,都是null。因为他的返回值是决定要不要调用postProcessPropertyValues方法中的一个因素(因为还有一个因素是mbd.getDependencyCheck());

        返回false :如果该方法返回false,并且不需要check,那么postProcessPropertyValues就会被忽略不执行;

        返回true : 如果返回true,postProcessPropertyValues就会被执行

4、属性修改

        方法名称 :InstantiationAwareBeanPostProcessor.PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName)。

        此方法可对属性值进行修改,修改范围包括添加、修改、删除操作;,如果实例化后置 postProcessAfterInstantiation() 方法返回false,那么该方法不会被调用。

5、给用户属性赋值

        用户属性指的是自定义的bean对象属性,像 User、Student、Teacher 、UserService、IndexService 这类的对象都是自定义bean对象,第5步主要给这类属性进行赋值操作,使用的是 AbstractAutowireCapableBeanFactory.populateBean() 方法进行赋值;

6、给容器属性赋值

        容器属性其实就是容器自带的属性,这些属性都是spring本来就有的;可以肯定的是,它们都是 Aware 接口的实现类。

 7、初始化前置

        方法名称: BeanPostProcessor.postProcessBeforeInitialization()

        在每一个 Bean 初始化之前执行的方法(有多少 Bean 调用多少次)  

8、执行初始化方法

        初始化方法有三个,分别是 添加了@PostConstruct 注解的方法、实现InitializingBean接口、在@bean注解上添加 initMethod属性。

        1.@PostConstruct 注解

package com.Spring.Boot.init;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

// @PostConstruct注解
@Component
public class ExtPostConstruct {

    /**
     * 被@PostConstruct修饰的方法会在构造函数之后。如果有多个则会执行多次
     * 注意: 如果spring 实现了 BeanPostProcessor接口的postProcessBeforeInitialization方法,该@PostConstruct注解会失效
     */
    @PostConstruct
    public void init() {
        System.out.println("第一个init...");
    }

    // 有多个会执行多次
    @PostConstruct
    public void init1() {
        System.out.println("第二个init1...");
    }

}

复制代码

        2.Spring 初始化方法之一,作用是在BeanFactory完成属性设置之后,执行自定义的初始化行为。执行顺序:在initMethod之前执行,在@PostConstruct之后执行

package com.Spring.Boot.init;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
public class ExtInitializingBean implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        // 一个 InitializingBean 执行一次
        // spring 初始化方法,作用是在BeanFactory完成属性设置之后,执行自定义的  初始化行为.
        // 执行顺序:在initMethod之前执行,在@PostConstruct之后执行
        System.out.println("InitializingBean");
    }
}

复制代码

        3.initMethod用于在bean初始化时指定执行方法,用来替代继承 InitializingBean接口。

package com.Spring.Boot.init.bean;

public class BeanTest {

    // 将要执行的初始化方法
    public void initMethod_1(){
        System.out.println("我是beanTest的init方法");
    }
}
package com.Spring.Boot.init;
import com.Spring.Boot.init.bean.BeanTest;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component()
public class InitMethod  {

    // 在@Bean注解上添加initMethod属性,指向类中的 initMethod_1 执行初始化方法
    @Bean(initMethod = "initMethod_1")
    public BeanTest getBeanTest(){
        return new BeanTest();
    }
}

9、初始化后置

        方法名称: BeanPostProcessor.postProcessAfterInitialization()

        在每一个 Bean 初始化之后执行的方法(有多少 Bean 调用多少次)

9、使用中

        到这一步,bean对象就已经完全创建好了,是一个完整对象了,并且正在被其他对象使用了。

9、销毁

        如果是单例模式,会先执行 DisposableBean.destroy()方法,然后在执行 destroy-Method 方法;

package com.Spring.Boot.init.destroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;

/**
 * 销毁方法
 */
@Component
public class ExtDisposableBean implements DisposableBean {
    @Override
    public void destroy() throws Exception {
        System.out.println("我被销毁了");
    }
}
package com.Spring.Boot.init;

import com.Spring.Boot.init.bean.BeanTest;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component()
public class InitMethod  {

    // 在@Bean注解上添加initMethod属性,指向类中的 initMethod_1 执行初始化方法
    // 在@Bean注解上添加destroyMethod属性,指向类中的 destroyMethod_1 执行销毁方法
    @Bean(initMethod = "initMethod_1",destroyMethod = "destroyMethod_1")
    public BeanTest getBeanTest(){
        return new BeanTest();
    }
}
BeanTest.java

package com.Spring.Boot.init.bean;

public class BeanTest {

    // 将要执行的初始化方法
    public void initMethod_1(){
        System.out.println("我是beanTest的init方法");
    }

    // 将要执行的销毁方法
    public void destroyMethod_1(){
        System.out.println("我是beanTest的init方法");
    }

}

参考文档:

        https://www.jianshu.com/p/6f7a82d869e7

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值