第一步:api
建立要實現AB測試的接口、實現類、controllerapp
@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(); } }
第二步:ide
建立RoutingBeanPostProcessor類實現接口BeanPostProcessor。註冊controller的bean時,對使用@RoutingInject註解的接口,建立動態代理類實現類。在使用該接口時,經過invoke方法建立接口實現類。post
對接口設置動態代理類註解:測試
@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface RoutingInject { }
開關設置註解:ui
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface RoutingSwitch { String value(); }
bean初始化後,對使用RoutingInject的註解類,進行處理後置處理this
@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 } }
第三步:blog
測試接口正常接口