zoukankan      html  css  js  c++  java
  • spring aop 一个挡板例子

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * 定义一个挡板
     */
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface BaffleAn {
    
    }
    import javassist.ClassClassPath;
    import javassist.ClassPool;
    import javassist.CtClass;
    import javassist.CtMethod;
    import javassist.bytecode.CodeAttribute;
    import javassist.bytecode.LocalVariableAttribute;
    import javassist.bytecode.MethodInfo;
    
    
    /**
     * 挡板切面类
     */
    @EnableAspectJAutoProxy
    @Component
    @Aspect
    public class BaffleAnAspect {
    
        @Pointcut("@annotation(BaffleAn)")
        public void point() {}
        
        @Around("point()")
        public Object around(ProceedingJoinPoint point) throws Throwable {
            return BaffleAnService.handler(point);
        }
        
        public static Map<String,Object> getFieldsNameValueMap(JoinPoint joinPoint) throws Exception {   
            Object[] args = joinPoint.getArgs();
            String classType = joinPoint.getTarget().getClass().getName();    
            Class<?> clazz = Class.forName(classType);    
            String clazzName = clazz.getName();    
            String methodName = joinPoint.getSignature().getName(); //获取方法名称   
            Map<String,Object > map=new HashMap<String,Object>();  
            ClassPool pool = ClassPool.getDefault();    
            ClassClassPath classPath = new ClassClassPath(BaffleAnAspect.class);    
            pool.insertClassPath(classPath);    
            CtClass cc = pool.get(clazzName);    
            CtMethod cm = cc.getDeclaredMethod(methodName);    
            MethodInfo methodInfo = cm.getMethodInfo();  
            CodeAttribute codeAttribute = methodInfo.getCodeAttribute();    
            LocalVariableAttribute attr = (LocalVariableAttribute) codeAttribute.getAttribute(LocalVariableAttribute.tag);    
            if (attr == null) {    
                throw new RuntimeException();
            }    
            int pos = Modifier.isStatic(cm.getModifiers()) ? 0 : 1;    
            for (int i = 0; i < cm.getParameterTypes().length; i++){    
                map.put( attr.variableName(i + pos),args[i]);//paramNames即参数名    
            }    
            return map;    
        }    
    }
    import java.util.Map;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    /**
     * 
        挡板处理类
     */
    public class BaffleAnService {
    
        public static Object handler(ProceedingJoinPoint point) throws Throwable {
            Object obj=null;
            Map<String,Object> map=BaffleAnAspect.getFieldsNameValueMap(point);
            if(map == null) {
                obj=point.proceed();
                return obj;
            }
            if(map.containsKey("AA")) {
                //挡板业务处理
            }
            obj=point.proceed();
            return obj;
            
        }
    }
  • 相关阅读:
    C++基础知识篇:C++ 存储类
    听说高手都用记事本写C语言代码?那你知道怎么编译运行吗?
    培训机构出来的程序员和科班比?看看这个科班毕业生怎么说~
    C++基础知识篇:C++ 修饰符类型
    从大学毕业到就业,程序员的人生如何走过?30岁以后的开发人员路在何方?
    终于有人把鸿蒙OS讲明白了,大佬讲解!快收藏!
    C++基础知识篇:C++ 常量
    Portrait Matting
    Deep-Trimap-Generation-for-Automatic-Video-Matting-using-GAN
    Automatic Trimap Generator
  • 原文地址:https://www.cnblogs.com/huzi007/p/11481868.html
Copyright © 2011-2022 走看看