spring源码--第七个后置处理器的使用:初始化方法

本文深入探讨了Spring框架中三种初始化方法的源码,包括@PostConstruct、InitializingBean接口的afterPropertiesSet()以及init-method。详细解释了它们的优先级和执行流程,特别指出@PostConstruct和@PreDestroy注解是通过后置处理器处理的,而其他两种方式则在invokeInitMethods()中完成。文章还分析了销毁方法的调用顺序和原理。
摘要由CSDN通过智能技术生成

目的

spring提供了三种初始化方法:
1、@PostConstruct、@PreDestory
2、实现 InitializingBean DisposableBean 接口,并实现对应的afterPropertiesSet()和destroy()方法
3、设置init-method和destory-method
本文主要解析三个初始化和销毁方法的源码

只是有一个点需要注意:@PostConstruct、@PreDestory这两个注解是通过后置处理器完成处理的 --> org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor#postProcessBeforeInitialization

其他两种方式,不是通过后置处理器完成的

源码

1、三种初始化方法的优先级从高到低;在spring官方文档也有明确指出

Multiple lifecycle mechanisms configured for the same bean, with
different initialization methods, are called as follows:
  Methods annotated with @PostConstruct
  afterPropertiesSet() as defined by the InitializingBean callback interface
 A custom configured init() method
Destroy methods are called in the same order:
  Methods annotated with @PreDestroy
  destroy() as defined by the DisposableBean callback interface
  A custom configured destroy() method

优先级之所以是这样,和源码中对三种配置方式处理的优先级有关系

测试代码

public class UserService04 implements InitializingBean,DisposableBean {
   

    public UserService04() {
   
    }

    @Override
    public void destroy() throws Exception {
   
        System.out.println("实现接口DisposableBean销毁");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
   
        System.out.println("实现接口InitializingBean初始化");
    }

    public void init(){
   
        System.out.println("initMethod");
    }

    public void destroyConfig(){
   
        System.out.println("destroyMethod");
    }

    @PostConstruct
    public void testPost(){
   
        System.out.println("注解PostConstruct init");
    }

    @PreDestroy
    public void testDestroy(){
   
        System.out.println("注解PreDestroy 销毁");
   
Bean的后置处理器是一种特殊的接口,即BeanPostProcessor接口。它定义了两个方法:postProcessBeforeInitialization和postProcessAfterInitialization。\[1\]这两个方法分别在Bean的初始化之前和之后被调用,可以在这两个方法中对Bean进行一些额外的处理操作。在Spring容器中,BeanFactory和ApplicationContext对待Bean的后置处理器有一些不同。ApplicationContext容器会自动检测配置文件中实现了BeanPostProcessor接口的,并将其注册为后置处理器,而BeanFactory容器需要通过代码显式注册后置处理器。\[2\]后置处理器可以用来修改Bean的属性或行为,例如在postProcessAfterInitialization方法中根据条件修改Bean的属性值。\[3\]通过实现BeanPostProcessor接口并重写相应的方法,我们可以自定义后置处理器来对Bean进行额外的处理。 #### 引用[.reference_title] - *1* [spring源码-bean的后置处理器](https://blog.csdn.net/sunqingzhong44/article/details/128284663)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Spring之BeanPostProcessor(后置处理器)介绍](https://blog.csdn.net/qq_38526573/article/details/88086752)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [[spring学习]5、bean的后置处理器(BeanPostProcessor)](https://blog.csdn.net/m0_51545690/article/details/125189749)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值