Spring?BeanPostProcessor后處理器源碼解析
Spring后處理器-BeanPostProcessor
- Bean被實(shí)例化后,到最終緩存到名為singletonObjects單例池之前,中間會(huì)經(jīng)過(guò)bean的初始化過(guò)程((該后處理器的執(zhí)行時(shí)機(jī))),例如:屬性的填充、初始化方法init的執(zhí)行等,其中有一個(gè)對(duì)外拓展的點(diǎn)BeanPostProcessor,我們稱(chēng)之為bean后處理器。與上文bean工廠后處理器相似,它也是一個(gè)接口,實(shí)現(xiàn)了該接口并被容器管理的BeanPostProcessor(即在配置文件中對(duì)其進(jìn)行配置),會(huì)在流程節(jié)點(diǎn)上被Spring自動(dòng)調(diào)用。
- BeanPostProcessor接口代碼如下
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by FernFlower decompiler) // package org.springframework.beans.factory.config; import org.springframework.beans.BeansException; import org.springframework.lang.Nullable; 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; } }
創(chuàng)建實(shí)現(xiàn)該接口(BeanPsotProcessor)的類(lèi),要在配置文件中進(jìn)行管理
快捷鍵 ctrl + insert 重寫(xiě)接口方法
package com.example.PostProcessor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName + ":postProcessBeforeInitialization"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName + ":postProcessAfterInitialization"); return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName); } }
測(cè)試類(lèi)代碼
package com.example.Test; import com.example.Service.Impl.UserServiceImpl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApplicationContext { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); System.out.println(context.getBean(UserServiceImpl.class)); } }
運(yùn)行結(jié)果如下
展示了該后處理器的執(zhí)行時(shí)機(jī)
Before和After執(zhí)行時(shí)機(jī)
在Bean實(shí)例化過(guò)程可以配置相關(guān)對(duì)于bean對(duì)象的操作方法,具體間往期文章:Bean的配置
注冊(cè)為bean的類(lèi)
package com.example.Service.Impl; import com.example.DAO.UserDAO; import com.example.Service.UserService; import org.springframework.beans.factory.InitializingBean; import java.util.List; import java.util.Map; import java.util.Set; public class UserServiceImpl implements UserService, InitializingBean { // todo 無(wú)參構(gòu)造方法 public UserServiceImpl() { System.out.println("UserServiceImpl實(shí)例化"); } // todo 自定義初始化方法 public void init() { System.out.println("自定義初始化方法init()"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("屬性設(shè)置之后執(zhí)行afterPropertiesSet()"); } }
實(shí)現(xiàn)bean后處理器的類(lèi)
package com.example.PostProcessor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName + ":postProcessBeforeInitialization"); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName + ":postProcessAfterInitialization"); return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName); } }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean class="com.example.PostProcessor.MyBeanPostProcessor"></bean> <bean id="userService" class="com.example.Service.Impl.UserServiceImpl" init-method="init"> </beans>
測(cè)試類(lèi)
package com.example.Test; import com.example.Service.Impl.UserServiceImpl; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApplicationContext { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); System.out.println(context.getBean(UserServiceImpl.class)); } }
運(yùn)行結(jié)果
小結(jié)
從上述運(yùn)行結(jié)果來(lái)看,首先完成bean對(duì)象的創(chuàng)建,然后執(zhí)行后處理器中的before方法,然后執(zhí)行屬性設(shè)置之后的方法,然后執(zhí)行自定義的初始化方法,最后執(zhí)行后處理器的after方法
案例
- 對(duì)Bean方法進(jìn)行執(zhí)行時(shí)間日志增強(qiáng)
- 要求
- Bean的方法執(zhí)行之前控制臺(tái)打印當(dāng)前時(shí)間
- Bean的方法執(zhí)行之后控制臺(tái)打印當(dāng)前時(shí)間
- 分析
- 對(duì)方法進(jìn)行增強(qiáng)主要就是代理設(shè)計(jì)模式和包裝設(shè)計(jì)模式
- 由于Bean方法不確定,所以使用動(dòng)態(tài)代理在運(yùn)行期間執(zhí)行增強(qiáng)操作
- 在Bean實(shí)例創(chuàng)建完畢之后,進(jìn)入到單例之前,使用Proxy真實(shí)的目標(biāo)bean
- 具體代碼如下
代理類(lèi)(實(shí)現(xiàn)了bean后處理接口)
package com.example.PostProcessor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Proxy; import java.util.Date; public class TimeLogBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { // todo 使用動(dòng)態(tài)代理對(duì)目標(biāo)bean進(jìn)行增強(qiáng),返回proxy對(duì)象,進(jìn)而存儲(chǔ)到單例池singletonObjects中 Object beanProxy = Proxy.newProxyInstance(bean.getClass().getClassLoader(), bean.getClass().getInterfaces(), (InvocationHandler) (proxy, method, args) -> { // 輸出開(kāi)始時(shí)間 System.out.println("方法" + method.getName() + "開(kāi)始執(zhí)行時(shí)間" + new Date()); // 執(zhí)行目標(biāo)方法 Object rs = method.invoke(bean, args); // 輸出結(jié)束時(shí)間 System.out.println("方法" + method.getName() + "結(jié)束執(zhí)行時(shí)間" + new Date()); return rs; }); return beanProxy;// 將增強(qiáng)的bean存入單例池中 } }
bean對(duì)象對(duì)應(yīng)的類(lèi)
package com.example.Service.Impl; import com.example.Service.UserService; public class UserServiceImpl implements UserService { public void show() { System.out.println("show......"); } }
接口類(lèi)
package com.example.Service; public interface UserService { public void show(); }
配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="com.example.PostProcessor.TimeLogBeanPostProcessor"></bean> <bean id="userService" class="com.example.Service.Impl.UserServiceImpl"> </bean> </beans>
測(cè)試類(lèi)
package com.example.Test; import com.example.Service.UserService; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApplicationContext { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("application.xml"); UserService UserServiceBean = (UserService) context.getBean(UserService.class); UserServiceBean.show(); } }
運(yùn)行結(jié)果
Bean實(shí)例化基本流程圖
首先通過(guò)Reader讀取配置文件,解析bean標(biāo)簽,然后將每個(gè)bean標(biāo)簽變成beanDefinition對(duì)象存儲(chǔ)到beanDefinitionMap中,然后經(jīng)過(guò)所有的BeanFactoryPostProcessor(bean工廠后處理器),再?gòu)腗ap中取出每個(gè)beanDefiniton對(duì)象,通過(guò)反射變成Object對(duì)象,創(chuàng)建完對(duì)象后,經(jīng)過(guò)beanPoatProcessor中的before方法和after方法(之間還存在bean中的init方法,后面會(huì)講述),最終存入單例池中。
到此這篇關(guān)于Spring BeanPostProcessor后處理器的文章就介紹到這了,更多相關(guān)Spring BeanPostProcessor后處理器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 關(guān)于Spring BeanPostProcessor的執(zhí)行順序
- Spring BeanPostProcessor(后置處理器)的用法
- SpringBoot之通過(guò)BeanPostProcessor動(dòng)態(tài)注入ID生成器案例詳解
- Spring容器的創(chuàng)建過(guò)程之如何注冊(cè)BeanPostProcessor詳解
- 詳解使用Spring的BeanPostProcessor優(yōu)雅的實(shí)現(xiàn)工廠模式
- Spring中的后置處理器BeanPostProcessor詳解
- Spring BeanPostProcessor接口使用詳解
- 解析Java的Spring框架的BeanPostProcessor發(fā)布處理器
- spring中BeanPostProcessor的作用和使用注意事項(xiàng)
相關(guān)文章
SpringCloud使用Feign實(shí)現(xiàn)遠(yuǎn)程調(diào)用流程詳細(xì)介紹
OpenFeign源于Netflix的Feign,是http通信的客戶(hù)端。屏蔽了網(wǎng)絡(luò)通信的細(xì)節(jié),直接面向接口的方式開(kāi)發(fā),讓開(kāi)發(fā)者感知不到網(wǎng)絡(luò)通信細(xì)節(jié)。所有遠(yuǎn)程調(diào)用,都像調(diào)用本地方法一樣完成2023-02-02java String、StringBuilder和StringBuffer的區(qū)別詳解
這篇文章主要介紹了java String、StringBuilder和StringBuffer的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01spring boot使用自定義配置的線程池執(zhí)行Async異步任務(wù)
這篇文章主要介紹了spring boot使用自定義配置的線程池執(zhí)行Async異步任務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01Java實(shí)現(xiàn)字符串倒序輸出的常用方法小結(jié)
這篇文章主要介紹了Java實(shí)現(xiàn)字符串倒序輸出的常用方法,通過(guò)三個(gè)實(shí)例從不同角度實(shí)現(xiàn)該功能,有不錯(cuò)的借鑒價(jià)值,需要的朋友可以參考下2014-09-09Java 多層嵌套JSON類(lèi)型數(shù)據(jù)全面解析
這篇文章主要介紹了Java 多層嵌套JSON類(lèi)型數(shù)據(jù)全面解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01簡(jiǎn)單了解Java創(chuàng)建線程兩種方法
這篇文章主要介紹了簡(jiǎn)單了解Java創(chuàng)建線程兩種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02springboot配置文件中使用${}注入值的兩種方式小結(jié)
這篇文章主要介紹了springboot配置文件中使用${}注入值的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java從JDK源碼角度對(duì)Object進(jìn)行實(shí)例分析
這篇文章主要介紹了Java從JDK源碼角度對(duì)Object進(jìn)行實(shí)例分析,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12SpringBoot同一個(gè)方法操作多個(gè)數(shù)據(jù)源保證事務(wù)一致性
本文探討了在Spring Boot應(yīng)用中,如何在同一個(gè)方法中操作多個(gè)數(shù)據(jù)源并保證事務(wù)的一致性,由于聲明式事務(wù)的限制,直接使用@Transactional注解無(wú)法滿足需求,文章介紹了解決方案:編程式事務(wù),它允許在代碼級(jí)別更靈活地管理事務(wù),確保多數(shù)據(jù)源操作的事務(wù)一致性2024-11-11