欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring探秘之如何妙用BeanPostProcessor

 更新時(shí)間:2022年01月10日 11:07:55   作者:圓圓仙人球  
BeanPostProcessor也稱為Bean后置處理器,它是Spring中定義的接口,在Spring容器的創(chuàng)建過程中會回調(diào)BeanPostProcessor中定義的兩個(gè)方法,這篇文章主要給大家介紹了關(guān)于Spring探秘之如何妙用BeanPostProcessor的相關(guān)資料,需要的朋友可以參考下

前言

最近,在給項(xiàng)目組使用Spring搭建Java項(xiàng)目基礎(chǔ)框架時(shí),發(fā)現(xiàn)使用Spring提供的BeanPostProcessor可以很簡單方便地解決很多看起來有點(diǎn)難解決的問題。本文將會通過一個(gè)真實(shí)案例來闡述BeanPostProcessor的用法

BeanPostProcessor簡介

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)類注冊到Spring IOC容器后,對于該Spring IOC容器所創(chuàng)建的每個(gè)bean實(shí)例在初始化方法(如afterPropertiesSet和任意已聲明的init方法)調(diào)用前,將會調(diào)用BeanPostProcessor中的postProcessBeforeInitialization方法,而在bean實(shí)例初始化方法調(diào)用完成后,則會調(diào)用BeanPostProcessor中的postProcessAfterInitialization方法,整個(gè)調(diào)用順序可以簡單示意如下:

--> Spring IOC容器實(shí)例化Bean
--> 調(diào)用BeanPostProcessor的postProcessBeforeInitialization方法
--> 調(diào)用bean實(shí)例的初始化方法
--> 調(diào)用BeanPostProcessor的postProcessAfterInitialization方法

可以看到,Spring容器通過BeanPostProcessor給了我們一個(gè)機(jī)會對Spring管理的bean進(jìn)行再加工。比如:我們可以修改bean的屬性,可以給bean生成一個(gè)動(dòng)態(tài)代理實(shí)例等等。一些Spring AOP的底層處理也是通過實(shí)現(xiàn)BeanPostProcessor來執(zhí)行代理包裝邏輯的。

BeanPostProcessor實(shí)戰(zhàn)

了解了BeanPostProcessor的相關(guān)知識后,下面我們來通過項(xiàng)目中的一個(gè)具體例子來體驗(yàn)一下它的神奇功效吧。

先介紹一下我們的項(xiàng)目背景吧:我們項(xiàng)目中經(jīng)常會涉及AB 測試,這就會遇到同一套接口會存在兩種不同實(shí)現(xiàn)。實(shí)驗(yàn)版本與對照版本需要在運(yùn)行時(shí)同時(shí)存在。下面用一些簡單的類來做一個(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測試的話,在使用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();
          }
     }
}

可以看到,這樣的代碼看起來十分不優(yōu)雅,并且如果AB測試的功能點(diǎn)很多的話,那項(xiàng)目中就會充斥著大量的這種重復(fù)性分支判斷,看到代碼就想死有木有?。。【S護(hù)代碼也將會是個(gè)噩夢。比如某個(gè)功能點(diǎn)AB測試完畢,需要把全部功能切換到V2版本,V1版本不再需要維護(hù),那么處理方式有兩種:

  • 把A版本代碼留著不管:這將會導(dǎo)致到處都是垃圾代碼從而造成代碼臃腫難以維護(hù)
  • 找到所有V1版本被調(diào)用的地方然后把相關(guān)分支刪掉:這很容易在處理代碼的時(shí)候刪錯(cuò)代碼從而造成生產(chǎn)事故。

怎么解決這個(gè)問題呢,我們先看代碼,后文再給出解釋:

@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)中開關(guān)的屬性名稱,應(yīng)用系統(tǒng)將會實(shí)時(shí)讀取配置系統(tǒng)中對應(yīng)開關(guān)的值來決定是調(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)在我們可以停下來對比一下封裝前后調(diào)用代碼了,是不是感覺改造后的代碼優(yōu)雅很多呢?那么這是怎么實(shí)現(xiàn)的呢,我們一起來揭開它的神秘面紗吧,請看代碼:

@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;
        }
    }
}

我簡要解釋一下思路:

  • 首先自定義了兩個(gè)注解:RoutingInjected、RoutingSwitch,前者的作用類似于我們常用的Autowired,聲明了該注解的屬性將會被注入一個(gè)路由代理類實(shí)例;后者的作用則是一個(gè)配置開關(guān),聲明了控制路由的開關(guān)屬性
  • 在RoutingBeanPostProcessor類中,我們在postProcessAfterInitialization方法中通過檢查bean中是否存在聲明了RoutingInjected注解的屬性,如果發(fā)現(xiàn)存在該注解則給該屬性注入一個(gè)動(dòng)態(tài)代理類實(shí)例
  • RoutingBeanProxyFactory類功能就是生成一個(gè)代理類實(shí)例,代理類的邏輯也比較簡單。版本路由支持到方法級別,即優(yōu)先檢查方法是否存在路由配置RoutingSwitch,方法不存在配置時(shí)才默認(rèn)使用類路由配置

好了,BeanPostProcessor的介紹就到這里了。不知道看過后大家有沒有得到一些啟發(fā)呢?

總結(jié)

到此這篇關(guān)于Spring探秘之如何妙用BeanPostProcessor的文章就介紹到這了,更多相關(guān)Spring妙用BeanPostProcessor內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論