這篇文章主要講解了“Spring使用BeanPostProcessor實現AB測試的方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Spring使用BeanPostProcessor實現AB測試的方法”吧!
創新互聯長期為成百上千家客戶提供的網站建設服務,團隊從業經驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯網生態環境。為上林企業提供專業的做網站、網站建設,上林網站改版等技術服務。擁有十多年豐富建站經驗和眾多成功案例,為您定制開發。
第一步:
創建要實現AB測試的接口、實現類、controller
@RoutingSwitch("hello.switch") public interface HelloService { @RoutingSwitch("B") String sayHello(); @RoutingSwitch("A") String sayHi(); }
@Service public class HelloServiceImplV1 implements HelloService { @Override public String sayHello() { String helloV1= "hello from V1"; System.out.println("hello from V1"); return helloV1; } @Override public String sayHi() { String hiV1= "hi from V1"; System.out.println("hi from V1"); return hiV1; } }
@Service public class HelloServiceImplV2 implements HelloService { @Override public String sayHello() { String helloV2 = "hello from V2"; System.out.println("hello from V2"); return helloV2; } @Override public String sayHi() { String hiV2 = "hi from V2"; System.out.println("hi from V2"); return hiV2; } }
@RestController @RequestMapping("/test") public class HelloControllerV2 { @RoutingInject private HelloService helloService; @GetMapping("/hello") public String sayHello() { return helloService.sayHello(); } @GetMapping("/hi") public String sayHi() { return helloService.sayHi(); } }
第二步:
創建RoutingBeanPostProcessor類實現接口BeanPostProcessor。注冊controller的bean時,對使用@RoutingInject注解的接口,創建動態代理類實現類。在使用該接口時,通過invoke方法創建接口實現類。
對接口設置動態代理類注解:
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface RoutingInject { }
開關設置注解:
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface RoutingSwitch { String value(); }
bean初始化后,對使用RoutingInject的注解類,進行處理后置處理
@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(RoutingInject.class)) { if(!f.getType().isInterface()) { throw new BeanCreationException("RoutingInject field must be declared as an interface:" + "@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 = applicationContext.getBeansOfType(type); field.setAccessible(true); if(candidates.size() == 1) { field.set(bean, candidates.entrySet().iterator().next()); }else if(candidates.size() == 2) { Object proxy = RoutingBeanProxyFactory.createProxy(type, candidates); field.set(bean, proxy); }else{ throw new IllegalAccessException("Find more bean 2 bean for type: " + type); } } }
代理工程實現類:
public class RoutingBeanProxyFactory { public static Object createProxy(Class targetClass, Map<String, Object> beans) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setInterfaces(new Class[]{targetClass}); proxyFactory.addAdvice(new VersionRoutingMethodInterceptor(targetClass, beans)); return proxyFactory.getProxy(); } static class VersionRoutingMethodInterceptor implements MethodInterceptor { private String classSwitch; private Object beanSwitchOn; private Object beanSwitchOff; 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.beanSwitchOn = beans.get(this.buildBeanName(interfaceName, true)); this.beanSwitchOff = 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(getTargetName(switchName), invocation.getArguments()); } public Object getTargetName(String switchName) { boolean switchOn; if(RoutingVersion.A.name().equals(switchName)) { switchOn = false; }else{ switchOn = true; } return switchOn ? beanSwitchOn : beanSwitchOff; } } enum RoutingVersion{ A,B } }
第三步:
測試接口正常
感謝各位的閱讀,以上就是“Spring使用BeanPostProcessor實現AB測試的方法”的內容了,經過本文的學習后,相信大家對Spring使用BeanPostProcessor實現AB測試的方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創新互聯,小編將為大家推送更多相關知識點的文章,歡迎關注!
當前名稱:Spring使用BeanPostProcessor實現AB測試的方法
本文來源:http://vcdvsql.cn/article10/pooigo.html
成都網站建設公司_創新互聯,為您提供網站導航、App開發、App設計、虛擬主機、網站改版、外貿網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯