Spring探秘之如何妙用BeanPostProcessor
前言
最近,在給項(xiàng)目組使用Spring搭建Java項(xiàng)目基礎(chǔ)框架時(shí),發(fā)現(xiàn)使用Spring提供的BeanPostProcessor可以很簡(jiǎn)單方便地解決很多看起來(lái)有點(diǎn)難解決的問(wèn)題。本文將會(huì)通過(guò)一個(gè)真實(shí)案例來(lái)闡述BeanPostProcessor的用法
BeanPostProcessor簡(jiǎn)介
BeanPostProcessor是Spring IOC容器給我們提供的一個(gè)擴(kuò)展接口。接口聲明如下:
public interface BeanPostProcessor {
//bean初始化方法調(diào)用前被調(diào)用
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
//bean初始化方法調(diào)用后被調(diào)用
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
如上接口聲明所示,BeanPostProcessor接口有兩個(gè)回調(diào)方法。當(dāng)一個(gè)BeanPostProcessor的實(shí)現(xiàn)類注冊(cè)到Spring IOC容器后,對(duì)于該Spring IOC容器所創(chuàng)建的每個(gè)bean實(shí)例在初始化方法(如afterPropertiesSet和任意已聲明的init方法)調(diào)用前,將會(huì)調(diào)用BeanPostProcessor中的postProcessBeforeInitialization方法,而在bean實(shí)例初始化方法調(diào)用完成后,則會(huì)調(diào)用BeanPostProcessor中的postProcessAfterInitialization方法,整個(gè)調(diào)用順序可以簡(jiǎn)單示意如下:
--> Spring IOC容器實(shí)例化Bean
--> 調(diào)用BeanPostProcessor的postProcessBeforeInitialization方法
--> 調(diào)用bean實(shí)例的初始化方法
--> 調(diào)用BeanPostProcessor的postProcessAfterInitialization方法
可以看到,Spring容器通過(guò)BeanPostProcessor給了我們一個(gè)機(jī)會(huì)對(duì)Spring管理的bean進(jìn)行再加工。比如:我們可以修改bean的屬性,可以給bean生成一個(gè)動(dòng)態(tài)代理實(shí)例等等。一些Spring AOP的底層處理也是通過(guò)實(shí)現(xiàn)BeanPostProcessor來(lái)執(zhí)行代理包裝邏輯的。
BeanPostProcessor實(shí)戰(zhàn)
了解了BeanPostProcessor的相關(guān)知識(shí)后,下面我們來(lái)通過(guò)項(xiàng)目中的一個(gè)具體例子來(lái)體驗(yàn)一下它的神奇功效吧。
先介紹一下我們的項(xiàng)目背景吧:我們項(xiàng)目中經(jīng)常會(huì)涉及AB 測(cè)試,這就會(huì)遇到同一套接口會(huì)存在兩種不同實(shí)現(xiàn)。實(shí)驗(yàn)版本與對(duì)照版本需要在運(yùn)行時(shí)同時(shí)存在。下面用一些簡(jiǎn)單的類來(lái)做一個(gè)示意:
public class HelloService{
void sayHello();
void sayHi();
}
HelloService有以下兩個(gè)版本的實(shí)現(xiàn):
@Service
public class HelloServiceImplV1 implements HelloService{
public void sayHello(){
System.out.println("Hello from V1");
}
public void sayHi(){
System.out.println("Hi from V1");
}
}
@Service
public class HelloServiceImplV2 implements HelloService{
public void sayHello(){
System.out.println("Hello from V2");
}
public void sayHi(){
System.out.println("Hi from V2");
}
}
做AB測(cè)試的話,在使用BeanPostProcessor封裝前,我們的調(diào)用代碼大概是像下面這樣子的:
@Controller
public class HelloController{
@Autowird
private HelloServiceImplV1 helloServiceImplV1;
@Autowird
private HelloServiceImplV2 helloServiceImplV2;
public void sayHello(){
if(getHelloVersion()=="A"){
helloServiceImplV1.sayHello();
}else{
helloServiceImplV2.sayHello();
}
}
public void sayHi(){
if(getHiVersion()=="A"){
helloServiceImplV1.sayHi();
}else{
helloServiceImplV2.sayHi();
}
}
}
可以看到,這樣的代碼看起來(lái)十分不優(yōu)雅,并且如果AB測(cè)試的功能點(diǎn)很多的話,那項(xiàng)目中就會(huì)充斥著大量的這種重復(fù)性分支判斷,看到代碼就想死有木有?。?!維護(hù)代碼也將會(huì)是個(gè)噩夢(mèng)。比如某個(gè)功能點(diǎn)AB測(cè)試完畢,需要把全部功能切換到V2版本,V1版本不再需要維護(hù),那么處理方式有兩種:
- 把A版本代碼留著不管:這將會(huì)導(dǎo)致到處都是垃圾代碼從而造成代碼臃腫難以維護(hù)
- 找到所有V1版本被調(diào)用的地方然后把相關(guān)分支刪掉:這很容易在處理代碼的時(shí)候刪錯(cuò)代碼從而造成生產(chǎn)事故。
怎么解決這個(gè)問(wèn)題呢,我們先看代碼,后文再給出解釋:
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface RoutingInjected{
}
@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface RoutingSwitch{
/**
* 在配置系統(tǒng)中開(kāi)關(guān)的屬性名稱,應(yīng)用系統(tǒng)將會(huì)實(shí)時(shí)讀取配置系統(tǒng)中對(duì)應(yīng)開(kāi)關(guān)的值來(lái)決定是調(diào)用哪個(gè)版本
* @return
*/
String value() default "";
}
@RoutingSwitch("hello.switch")
public class HelloService{
@RoutingSwitch("A")
void sayHello();
void sayHi();
}
@Controller
public class HelloController{
@RoutingInjected
private HelloService helloService;
public void sayHello(){
this.helloService.sayHello();
}
public void sayHi(){
this.helloService.sayHi();
}
}
現(xiàn)在我們可以停下來(lái)對(duì)比一下封裝前后調(diào)用代碼了,是不是感覺(jué)改造后的代碼優(yōu)雅很多呢?那么這是怎么實(shí)現(xiàn)的呢,我們一起來(lái)揭開(kāi)它的神秘面紗吧,請(qǐng)看代碼:
@Component
public class RoutingBeanPostProcessor implements BeanPostProcessor {
@Autowired
private ApplicationContext applicationContext;
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
Class clazz = bean.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field f : fields) {
if (f.isAnnotationPresent(RoutingInjected.class)) {
if (!f.getType().isInterface()) {
throw new BeanCreationException("RoutingInjected field must be declared as an interface:" + f.getName()
+ " @Class " + clazz.getName());
}
try {
this.handleRoutingInjected(f, bean, f.getType());
} catch (IllegalAccessException e) {
throw new BeanCreationException("Exception thrown when handleAutowiredRouting", e);
}
}
}
return bean;
}
private void handleRoutingInjected(Field field, Object bean, Class type) throws IllegalAccessException {
Map<String, Object> candidates = this.applicationContext.getBeansOfType(type);
field.setAccessible(true);
if (candidates.size() == 1) {
field.set(bean, candidates.values().iterator().next());
} else if (candidates.size() == 2) {
Object proxy = RoutingBeanProxyFactory.createProxy(type, candidates);
field.set(bean, proxy);
} else {
throw new IllegalArgumentException("Find more than 2 beans for type: " + type);
}
}
}
public class RoutingBeanProxyFactory {
public static Object createProxy(Class targetClass, Map<String, Object> beans) {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setInterfaces(targetClass);
proxyFactory.addAdvice(new VersionRoutingMethodInterceptor(targetClass, beans));
return proxyFactory.getProxy();
}
static class VersionRoutingMethodInterceptor implements MethodInterceptor {
private String classSwitch;
private Object beanOfSwitchOn;
private Object beanOfSwitchOff;
public VersionRoutingMethodInterceptor(Class targetClass, Map<String, Object> beans) {
String interfaceName = StringUtils.uncapitalize(targetClass.getSimpleName());
if(targetClass.isAnnotationPresent(RoutingSwitch.class)){
this.classSwitch =((RoutingSwitch)targetClass.getAnnotation(RoutingSwitch.class)).value();
}
this.beanOfSwitchOn = beans.get(this.buildBeanName(interfaceName, true));
this.beanOfSwitchOff = beans.get(this.buildBeanName(interfaceName, false));
}
private String buildBeanName(String interfaceName, boolean isSwitchOn) {
return interfaceName + "Impl" + (isSwitchOn ? "V2" : "V1");
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
String switchName = this.classSwitch;
if (method.isAnnotationPresent(RoutingSwitch.class)) {
switchName = method.getAnnotation(RoutingSwitch.class).value();
}
if (StringUtils.isBlank(switchName)) {
throw new IllegalStateException("RoutingSwitch's value is blank, method:" + method.getName());
}
return invocation.getMethod().invoke(getTargetBean(switchName), invocation.getArguments());
}
public Object getTargetBean(String switchName) {
boolean switchOn;
if (RoutingVersion.A.equals(switchName)) {
switchOn = false;
} else if (RoutingVersion.B.equals(switchName)) {
switchOn = true;
} else {
switchOn = FunctionSwitch.isSwitchOpened(switchName);
}
return switchOn ? beanOfSwitchOn : beanOfSwitchOff;
}
}
}
我簡(jiǎn)要解釋一下思路:
- 首先自定義了兩個(gè)注解:RoutingInjected、RoutingSwitch,前者的作用類似于我們常用的Autowired,聲明了該注解的屬性將會(huì)被注入一個(gè)路由代理類實(shí)例;后者的作用則是一個(gè)配置開(kāi)關(guān),聲明了控制路由的開(kāi)關(guān)屬性
- 在RoutingBeanPostProcessor類中,我們?cè)趐ostProcessAfterInitialization方法中通過(guò)檢查bean中是否存在聲明了RoutingInjected注解的屬性,如果發(fā)現(xiàn)存在該注解則給該屬性注入一個(gè)動(dòng)態(tài)代理類實(shí)例
- RoutingBeanProxyFactory類功能就是生成一個(gè)代理類實(shí)例,代理類的邏輯也比較簡(jiǎn)單。版本路由支持到方法級(jí)別,即優(yōu)先檢查方法是否存在路由配置RoutingSwitch,方法不存在配置時(shí)才默認(rèn)使用類路由配置
好了,BeanPostProcessor的介紹就到這里了。不知道看過(guò)后大家有沒(méi)有得到一些啟發(fā)呢?
總結(jié)
到此這篇關(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后處理器源碼解析
- 詳解使用Spring的BeanPostProcessor優(yōu)雅的實(shí)現(xiàn)工廠模式
- Spring源碼解析之BeanPostProcessor知識(shí)總結(jié)
- Spring BeanPostProcessor源碼示例解析
- Spring注解驅(qū)動(dòng)之BeanPostProcessor后置處理器講解
- Spring組件初始化擴(kuò)展點(diǎn):BeanPostProcessor
相關(guān)文章
LinkedList學(xué)習(xí)示例模擬堆棧與隊(duì)列數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了LinkedList學(xué)習(xí)示例,模擬一個(gè)堆棧與隊(duì)列數(shù)據(jù)結(jié)構(gòu),大家參考使用吧2014-01-01
SpringBoot之自定義啟動(dòng)異常堆棧信息打印方式
這篇文章主要介紹了SpringBoot之自定義啟動(dòng)異常堆棧信息打印方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java如何用一個(gè)統(tǒng)一結(jié)構(gòu)接收成員名稱不固定的數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Java如何用一個(gè)統(tǒng)一結(jié)構(gòu)接收成員名稱不固定的數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
Ubuntu 15下安裝Eclipse經(jīng)驗(yàn)分享
這篇文章主要為大家分享了Ubuntu 15下安裝Eclipse經(jīng)驗(yàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

