controller版本号控制

该博客介绍了如何使用SpringMVC、AOP、反射和注解来实现Controller接口的版本控制。通过配置切入点、编写切面类以及处理不同版本的方法,实现了根据请求的version动态调用对应版本的Controller方法。
摘要由CSDN通过智能技术生成
  1. springmvc+aop+reflect+annotation
  2. 实现思路:将controller接口配置成切点,然后在切面环绕通知中,获取到请求的version,然后反射执行对应版本号的controller中的方法。
  3. 配置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>
  4. 编写自己的切面,执行环绕通知
    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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值