Spring?BeanPostProcessor后處理器源碼解析
Spring后處理器-BeanPostProcessor
- Bean被實(shí)例化后,到最終緩存到名為singletonObjects單例池之前,中間會(huì)經(jīng)過(guò)bean的初始化過(guò)程((該后處理器的執(zhí)行時(shí)機(jī))),例如:屬性的填充、初始化方法init的執(zhí)行等,其中有一個(gè)對(duì)外拓展的點(diǎn)BeanPostProcessor,我們稱之為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)的類,要在配置文件中進(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è)試類代碼
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的類
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后處理器的類
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è)試類
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
- 具體代碼如下
代理類(實(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)的類
package com.example.Service.Impl;
import com.example.Service.UserService;
public class UserServiceImpl implements UserService {
public void show() {
System.out.println("show......");
}
}接口類
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è)試類
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)文章希望大家以后多多支持腳本之家!
- Spring BeanPostProcessor接口使用詳解
- Spring中的后置處理器BeanPostProcessor詳解
- SpringBoot之通過(guò)BeanPostProcessor動(dòng)態(tài)注入ID生成器案例詳解
- Spring BeanPostProcessor(后置處理器)的用法
- 詳解使用Spring的BeanPostProcessor優(yōu)雅的實(shí)現(xiàn)工廠模式
- Spring探秘之如何妙用BeanPostProcessor
- Spring源碼解析之BeanPostProcessor知識(shí)總結(jié)
- Spring BeanPostProcessor源碼示例解析
- Spring注解驅(qū)動(dòng)之BeanPostProcessor后置處理器講解
- Spring組件初始化擴(kuò)展點(diǎn):BeanPostProcessor
相關(guān)文章
Java Socket編程實(shí)例(五)- NIO UDP實(shí)踐
這篇文章主要講解Java Socket編程中NIO UDP的實(shí)例,希望能給大家做一個(gè)參考。2016-06-06
基于springboot處理date參數(shù)過(guò)程解析
這篇文章主要介紹了基于springboot處理date參數(shù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
IntelliJ IDEA 如何徹底刪除項(xiàng)目的步驟
本篇文章主要介紹了IntelliJ IDEA 如何徹底刪除項(xiàng)目的步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法
今天小編就為大家分享一篇java 二進(jìn)制數(shù)據(jù)與16進(jìn)制字符串相互轉(zhuǎn)化方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Spring中XML schema擴(kuò)展機(jī)制的深入講解
這篇文章主要給大家介紹了關(guān)于Spring中XML schema擴(kuò)展機(jī)制的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
springboot+Oauth2實(shí)現(xiàn)自定義AuthenticationManager和認(rèn)證path
本篇文章主要介紹了springboot+Oauth2實(shí)現(xiàn)自定義AuthenticationManager和認(rèn)證path,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Java詳解實(shí)現(xiàn)多線程的四種方式總結(jié)
哈哈!經(jīng)過(guò)一個(gè)階段的學(xué)習(xí),Java基礎(chǔ)知識(shí)學(xué)習(xí)終于到多線程了!Java多線程以及后面互斥鎖的概念都是Java基礎(chǔ)學(xué)習(xí)的難點(diǎn),所以我做了一個(gè)總結(jié),希望對(duì)大家也有幫助2022-07-07

