• some notes about spring aop


    1 . 
    timeCountIntecetor implements handlerInterceptor {
         preHandle(); postHandle(); afterComplete();
    }
     
     
     2 . 动态代理 by implement InvocationHandler (对接口)
    class MyProxy implements InvocationHandler
    {
        Object obj;
        public Object bind(Object obj)
        {
            this.obj = obj;
            return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj
                    .getClass().getInterfaces(), this);
        }
        @Override
        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable
        {
            System.out.println("I'm proxy!");
            Object res = method.invoke(obj, args);
            return res;
        }
    }
     
    public class DynamicProxy
    {
        public static void main(String[] args)
        {
            MyProxy myproxy = new MyProxy();
            HoseeDynamicimpl dynamicimpl = new HoseeDynamicimpl();
            HoseeDynamic proxy = (HoseeDynamic)myproxy.bind(dynamicimpl);
            System.out.println(proxy.sayhi());
        }
    }
    

      

     
     3 . 对类:
    @Aspect
    ServiceTimeCountAspect:
     
     
    @Pointcut("execution(* me.ele.jarch.aries.service.*.*(..))")
        private void serviceMethod() {
        }
     
    //  @Around("me.ele.jarch.aries.aspect.ServiceTimeCountAspect.serviceMethod()")
    //    @Around("serviceMethod() || repositoryMethod()")
        @Around("serviceMethod()")
        public Object logServiceMethodRunningTime(ProceedingJoinPoint pjp)
                throws Throwable {
            // start stopwatch
            StopWatch watch = new StopWatch();
            watch.start();
     
            Object retVal = pjp.proceed();
     
            // stop stopwatch
            watch.stop();
            Long time = watch.getTotalTimeMillis();
            String methodName = pjp.getSignature().getName();
     
            logger.info("service method: {} time count : {}", methodName, time);
     
            return retVal;
        }
     
    Spring AOP 会动态选择使用 JDK 动态代理、CGLIB 来生成 AOP 代理,如果目标类实现了接口,Spring AOP 则无需 CGLIB 的支持,直接使用 JDK 提供的 Proxy 和 InvocationHandler 来生成 AOP 代理即可。
  • 相关阅读:
    十一、VueJs 填坑日记之使用Amaze ui调整列表和内容页面
    十、VueJs 填坑日记之在项目中使用Amaze UI
    九、VueJs 填坑日记之在项目中使用jQuery
    八、VueJs 填坑日记之参数传递及内容页面的开发
    七、VueJs 填坑日记之渲染一个列表
    六、VueJs 填坑日记之初识*.Vue文件
    jmeter连接mysql数据库
    Markdown基础语法
    Python自动化准备工作(pycharm安装)
    Android手机测试环境搭建
  • 原文地址:https://www.cnblogs.com/mywy/p/6130144.html
Copyright © 2020-2023  润新知