• spring aop 基础1


    作用:增强方法

    本例基于 反射+注解+aspect

    流程

      1:创建注解(标识哪个方法使用aop,也可以直接使用表达式)  

      2:创建切面

      3:创建方法,使用注解标记

    1:创建注解

    /**
     * 标记**方法使用 WebAspect 切面
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)     //作用与方法
    public @interface WebAnnotation {
    
    
    }

    2:创建切面

    @Aspect
    @Component
    public class WebAspect {
    
        /**
         * 定义切点
         */
        @Pointcut(value = "@annotation(com.draymond.aop2.annotation.WebAnnotation)")
        private void pointCut() {
        }
    
        @Before(value = "pointCut()")
        public void before(JoinPoint point) {                                   //  execution(void com.draymond.aop2.service.UserService.findById(int))
            System.out.println(" WebAspect before ");
            String kind = point.getKind();                                      //  method-execution 获取连接点的类型
            Object[] args = point.getArgs();                                    //  [1] 参数的值
            Object target = point.getTarget();
            Class<?> clazz = target.getClass();                                 //  class com.draymond.aop2.service.UserService
            MethodSignature signature = (MethodSignature) point.getSignature();
            String signatureName = signature.getName();                         //  findById
            Class declaringType = signature.getDeclaringType();                 //  class com.draymond.aop2.service.UserService
            String declaringTypeName = signature.getDeclaringTypeName();        //  com.draymond.aop2.service.UserService
            String[] parameterNames = signature.getParameterNames();            //  [i] 参数名称
            Class[] parameterTypes = signature.getParameterTypes();             //  [int]
            Method method = signature.getMethod();                              //  public void com.draymond.aop2.service.UserService.findById(int)
            Class returnType = signature.getReturnType();                       //  void
    
            int modifiers = signature.getModifiers();                           // 1
    
            SourceLocation sourceLocation = point.getSourceLocation();          //  execution(void com.draymond.aop2.service.UserService.findById(int))
            JoinPoint.StaticPart staticPart = point.getStaticPart();            //  execution(void com.draymond.aop2.service.UserService.findById(int))
            Object aThis = point.getThis();                                     //  com.draymond.aop2.service.UserService@d90ec19
            String s = point.toLongString();                                    // execution(public void com.draymond.aop2.service.UserService.findById(int))
            String s1 = point.toShortString();                                  // execution(UserService.findById(..))
            String s2 = point.toString();                                       // execution(void com.draymond.aop2.service.UserService.findById(int))
    
    
            System.out.println("-----------------");
            Map<String, Object> map = new HashMap<>();
            for (int i = 0; i < parameterNames.length; i++) {
                map.put(parameterNames[i], args[i]);                            //  {"int":1}   获取到方法的参数,以及对应的值
            }
    
            System.out.println(JsonUtils.toStringNoEx(map));
    
        }

    3:创建方法,使用注解标记

    @Service
    public class UserService {
        @WebAnnotation
        public void findById(int i) {
            System.out.println("执行了 UserService 的 findById 方法 ");
        }
    }

    4:controller中调用该方法,再请求接口

    补充:通知类型

        before

        after

        afterThrowing

        afterReturn

        around

      参数:jointPoint  ProceedingJoinPoint   

                 

      

  • 相关阅读:
    POJ 1703 Find them, Catch them
    POJ 2236 Wireless Network
    POJ 2010 Moo University
    POJ 2184 Cow Exhibition
    POJ 3280 Cheapest Palindrome
    POJ 3009 Curling 2.0
    POJ 3669 Meteor Shower
    POJ 2718 Smallest Difference
    POJ 3187 Backward Digit Sums
    POJ 3050 Hopscotch
  • 原文地址:https://www.cnblogs.com/draymond/p/12664685.html
Copyright © 2020-2023  润新知