• BeanPostProcessor和BeanFactoryPostProcessor简单介绍


    BeanPostProcessor 接口定义了一个你可以自己实现的回调方法,来实现你自己的实例化逻辑、依赖解决逻辑等,如果你想要在Spring完成对象实例化、配置、初始化之后实现自己的业务逻辑,你可以补充实现一个或多个BeanPostProcessor的实现。

    BeanFactoryPostProcessor的定义和BeanPostProcessor相似,有一个最主要的不同是:BeanFactoryPostProcessor可以对bean的配置信息进行操作;更确切的说Spring IOC容器允许BeanFactoryPostProcessor读取配置信息并且能够在容器实例化任何其他bean(所有的实现了BeanFactoryPostProcessor接口的类)之前改变配置信息

    BeanPostProcessor

    接口定义

    
    public interface BeanPostProcessor {
    
    
    	@Nullable
    	default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    		return bean;
    	}
    
    	
    	@Nullable
    	default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    		return bean;
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    postProcessBeforeInitialization和postProcessAfterInitialization
    入参是 bean示例和beanName,此方法内可以对bean进行处理并且返回一个对象,更改bean实例,例如代理,修改对象数据

    执行时机

    执行时机参考org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean(java.lang.String, java.lang.Object, org.springframework.beans.factory.support.RootBeanDefinition)

    if (mbd == null || !mbd.isSynthetic()) {
    			wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
    		}
    
    		try {
    			invokeInitMethods(beanName, wrappedBean, mbd);
    		}
    		catch (Throwable ex) {
    			throw new BeanCreationException(
    					(mbd != null ? mbd.getResourceDescription() : null),
    					beanName, "Invocation of init method failed", ex);
    		}
    		if (mbd == null || !mbd.isSynthetic()) {
    			wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
    		}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    即整个bean已经加载完毕,依赖的bean已经注入完毕,分别在,执行初始化方法前和方法后执行
    初始化方法指的是执行InitializingBean的afterPropertiesSet方法
    初始化方法指的是bean实现了InitializingBean接口,对应的方法为afterPropertiesSet

    BeanFactoryPostProcessor

    接口定义

    FunctionalInterface
    public interface BeanFactoryPostProcessor {
    
    	void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    入参就是beanFactory,可以对beanFactory进行修改
    例如通过beanFactory修改beanDefination,添加属性

    public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
     
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            System.out.println("调用MyBeanFactoryPostProcessor的postProcessBeanFactory");
            BeanDefinition bd = beanFactory.getBeanDefinition("myJavaBean");
            MutablePropertyValues pv =  bd.getPropertyValues();  
            if (pv.contains("remark")) {  
                pv.addPropertyValue("remark", "在BeanFactoryPostProcessor中修改之后的备忘信息");  
            }  
        }
     
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    执行时机

    在bean实例化之前执行,在invokeBeanFactoryPostProcessors中。

  • 相关阅读:
    【Servlet】实现Servlet程序
    ts的基本数组在项目组中的使用,如何使用
    redis集群模式
    干货 | 如何快速实现 BitSail Connector?
    【Flink源码篇】Flink提交流程之flink命令自定义参数的解析和命令行客户端的选择
    基于SpringBoot的美发门店管理系统
    博弈论——伯特兰德寡头模型(Bertrand Model)
    企业架构LNMP学习笔记45
    Synchronized锁详解
    0043-python爬虫学习第二天:beautifulsoup库
  • 原文地址:https://blog.csdn.net/qq_37436172/article/details/127834066