- springmvc+aop+reflect+annotation
- 实现思路:将controller接口配置成切点,然后在切面环绕通知中,获取到请求的version,然后反射执行对应版本号的controller中的方法。
- 配置springmvc的Controller为切入点
<aop:config> <aop:pointcut expression="execution(* com.*.*.*.controller.*Controller.*PlatformEntry(..))" id="platformMethodPoint"/> <aop:aspect ref="platformMethodAspect"> <aop:around method="aroundAdvice" pointcut-ref="platformMethodPoint"/> </aop:aspect> </aop:config>
编写自己的切面,执行环绕通知
public class PlatformMethodAspect{ protected Logger logger = LoggerFactory.getLogger(getClass()); private static final String METHOD_VERSION_BUSIDATA = "busiData"; private static final String METHOD_VERSION_BUSIDATA_BASE = "BASE"; private static final String METHOD_VERSION_REQUESTDATA = "requestData"; private static final String METHOD_VERSION_ELEMENT_NAME = "VERSION"; private Map<String,Map<String,String>> versionManager; private static final String SUPPORT_MIN_VERSION = "supportMinVersion"; private static final String DEFAULT_VERSION = "defaultVersion"; private WebApplicationContext appContext; /** * 获取到指定版本号的方法 * @param version 版本号 * @param clazz 目标Controller * @return */ public Method getMethodByVersionAndClass(String entryMethodName,String version,Class<?> clazz){ Method targetMethod = null; Method[] methods = clazz.getMethods(); if(null != methods && methods.length > 0){ for(Method method : methods){ PlatformMethod findAnnotation = AnnotationUtils.findAnnotation(method,PlatformMethod.class); if(null != findAnnotation && entryMethodName.equals(findAnnotation.entryHandlerName()) && version.equals(findAnnotation.version())){ targetMethod = method; break; } } } return targetMethod; } /** * 根据busiData获取version * @param handlerDataValue 业务数据 * @return 返回接口version * @throws DocumentException */ public String getVersionByBusiData(String handlerDataName,String handlerDataValue) throws DocumentException{ InputStream inputStream = new ByteArrayInputStream(handlerDataValue.getBytes()); SAXReader saxReader = new SAXReader(); Document document = null; try { document = saxReader.read(inputStre
controller版本号控制
最新推荐文章于 2024-07-25 18:00:20 发布
该博客介绍了如何使用SpringMVC、AOP、反射和注解来实现Controller接口的版本控制。通过配置切入点、编写切面类以及处理不同版本的方法,实现了根据请求的version动态调用对应版本的Controller方法。
摘要由CSDN通过智能技术生成