Java BeanPostProcessor與BeanFactoryPostProcessor基礎(chǔ)使用講解
前言
BeanPostProcessor 接口定義了一個(gè)你可以自己實(shí)現(xiàn)的回調(diào)方法,來實(shí)現(xiàn)你自己的實(shí)例化邏輯、依賴解決邏輯等,如果你想要在Spring完成對(duì)象實(shí)例化、配置、初始化之后實(shí)現(xiàn)自己的業(yè)務(wù)邏輯,你可以補(bǔ)充實(shí)現(xiàn)一個(gè)或多個(gè)BeanPostProcessor的實(shí)現(xiàn)。
BeanFactoryPostProcessor的定義和BeanPostProcessor相似,有一個(gè)最主要的不同是:BeanFactoryPostProcessor可以對(duì)bean的配置信息進(jìn)行操作;更確切的說Spring IOC容器允許BeanFactoryPostProcessor讀取配置信息并且能夠在容器實(shí)例化任何其他bean(所有的實(shí)現(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
入?yún)⑹?bean示例和beanName,此方法內(nèi)可以對(duì)bean進(jìn)行處理并且返回一個(gè)對(duì)象,更改bean實(shí)例,例如代理,修改對(duì)象數(shù)據(jù)
執(zhí)行時(shí)機(jī)
執(zhí)行時(shí)機(jī)參考
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); }
即整個(gè)bean已經(jīng)加載完畢,依賴的bean已經(jīng)注入完畢,分別在,執(zhí)行初始化方法前和方法后執(zhí)行
初始化方法指的是執(zhí)行InitializingBean的afterPropertiesSet方法
初始化方法指的是bean實(shí)現(xiàn)了InitializingBean接口,對(duì)應(yīng)的方法為afterPropertiesSet
BeanFactoryPostProcessor
接口定義
FunctionalInterface public interface BeanFactoryPostProcessor { void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }
入?yún)⒕褪莃eanFactory,可以對(duì)beanFactory進(jìn)行修改
例如通過beanFactory修改beanDefination,添加屬性
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor { public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println("調(diào)用MyBeanFactoryPostProcessor的postProcessBeanFactory"); BeanDefinition bd = beanFactory.getBeanDefinition("myJavaBean"); MutablePropertyValues pv = bd.getPropertyValues(); if (pv.contains("remark")) { pv.addPropertyValue("remark", "在BeanFactoryPostProcessor中修改之后的備忘信息"); } } }
執(zhí)行時(shí)機(jī)
在bean實(shí)例化之前執(zhí)行,在invokeBeanFactoryPostProcessors中。
到此這篇關(guān)于Java BeanPostProcessor與BeanFactoryPostProcessor基礎(chǔ)使用講解的文章就介紹到這了,更多相關(guān)Java BeanPostProcessor內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java?線程池狀態(tài)及狀態(tài)轉(zhuǎn)換
這篇文章主要介紹了java?線程池狀態(tài)及狀態(tài)轉(zhuǎn)換,Java里線程池的狀態(tài)和線程的狀態(tài)是完全不同的,具體有幾種狀態(tài)和哪些不同點(diǎn),下面文章詳細(xì)介紹,需要的小伙伴可以參考一下2022-05-05Java操作MongoDB插入數(shù)據(jù)進(jìn)行模糊查詢與in查詢功能
今天小編就為大家分享一篇關(guān)于Java操作MongoDB插入數(shù)據(jù)進(jìn)行模糊查詢與in查詢功能,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12Elasticsearch倒排索引詳解及實(shí)際應(yīng)用中的優(yōu)化
Elasticsearch(ES)使用倒排索引來加速文本的搜索速度,倒排索引之所以高效,主要是因?yàn)樗淖兞藬?shù)據(jù)的組織方式,使得查詢操作可以快速完成,這篇文章主要給大家介紹了關(guān)于Elasticsearch倒排索引詳解及實(shí)際應(yīng)用中優(yōu)化的相關(guān)資料,需要的朋友可以參考下2024-08-08Java語言實(shí)現(xiàn)簡單FTP軟件 FTP遠(yuǎn)程文件管理模塊實(shí)現(xiàn)(10)
這篇文章主要為大家詳細(xì)介紹了Java語言實(shí)現(xiàn)簡單FTP軟件,F(xiàn)TP遠(yuǎn)程文件管理模塊的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot2.0整合jackson配置日期格式化和反序列化的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11解決springboot項(xiàng)目啟動(dòng)報(bào)錯(cuò)Error creating bean with&nb
這篇文章主要介紹了解決springboot項(xiàng)目啟動(dòng)報(bào)錯(cuò)Error creating bean with name dataSourceScriptDatabaseInitializer問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2024-03-03