Java BeanPostProcessor與BeanFactoryPostProcessor基礎使用講解
前言
BeanPostProcessor 接口定義了一個你可以自己實現(xiàn)的回調方法,來實現(xiàn)你自己的實例化邏輯、依賴解決邏輯等,如果你想要在Spring完成對象實例化、配置、初始化之后實現(xiàn)自己的業(yè)務邏輯,你可以補充實現(xiàn)一個或多個BeanPostProcessor的實現(xiàn)。
BeanFactoryPostProcessor的定義和BeanPostProcessor相似,有一個最主要的不同是:BeanFactoryPostProcessor可以對bean的配置信息進行操作;更確切的說Spring IOC容器允許BeanFactoryPostProcessor讀取配置信息并且能夠在容器實例化任何其他bean(所有的實現(xiàn)了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; } }
postProcessBeforeInitialization和postProcessAfterInitialization
入參是 bean示例和beanName,此方法內可以對bean進行處理并且返回一個對象,更改bean實例,例如代理,修改對象數據
執(zhí)行時機
執(zhí)行時機參考
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); }
即整個bean已經加載完畢,依賴的bean已經注入完畢,分別在,執(zhí)行初始化方法前和方法后執(zhí)行
初始化方法指的是執(zhí)行InitializingBean的afterPropertiesSet方法
初始化方法指的是bean實現(xiàn)了InitializingBean接口,對應的方法為afterPropertiesSet
BeanFactoryPostProcessor
接口定義
FunctionalInterface public interface BeanFactoryPostProcessor { void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }
入參就是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中修改之后的備忘信息"); } } }
執(zhí)行時機
在bean實例化之前執(zhí)行,在invokeBeanFactoryPostProcessors中。
到此這篇關于Java BeanPostProcessor與BeanFactoryPostProcessor基礎使用講解的文章就介紹到這了,更多相關Java BeanPostProcessor內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java操作MongoDB插入數據進行模糊查詢與in查詢功能
今天小編就為大家分享一篇關于Java操作MongoDB插入數據進行模糊查詢與in查詢功能,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12Elasticsearch倒排索引詳解及實際應用中的優(yōu)化
Elasticsearch(ES)使用倒排索引來加速文本的搜索速度,倒排索引之所以高效,主要是因為它改變了數據的組織方式,使得查詢操作可以快速完成,這篇文章主要給大家介紹了關于Elasticsearch倒排索引詳解及實際應用中優(yōu)化的相關資料,需要的朋友可以參考下2024-08-08Java語言實現(xiàn)簡單FTP軟件 FTP遠程文件管理模塊實現(xiàn)(10)
這篇文章主要為大家詳細介紹了Java語言實現(xiàn)簡單FTP軟件,F(xiàn)TP遠程文件管理模塊的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04SpringBoot2.0整合jackson配置日期格式化和反序列化的實現(xiàn)
這篇文章主要介紹了SpringBoot2.0整合jackson配置日期格式化和反序列化的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11解決springboot項目啟動報錯Error creating bean with&nb
這篇文章主要介紹了解決springboot項目啟動報錯Error creating bean with name dataSourceScriptDatabaseInitializer問題,具有很好的參考價值,希望對大家有所幫助2024-03-03