美文网首页
Spring AOP学习笔记三

Spring AOP学习笔记三

作者: 代码potty | 来源:发表于2018-08-13 15:42 被阅读0次

Spring AOP三个实战例子:
1、通过自定义一个注释类,然后定位到方法上

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MusicAnnotation {
    }

然后注释到使用的方法上


image.png

2、通过@Before,@After,@AfterReturning,@AfterThrowing,@Around五种通知进行日志的管理

3、可以对一些接口或者方法,获取它的运行时间,如下所示

    @Around("checkAnno()")
        public Object AroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
    
            Object result = joinPoint.proceed();
    
            stopWatch.stop();
    
            System.out.println(stopWatch.getTotalTimeMillis());
    
            return result;
        }       

在需要的方法上注释,可以看到操作台显示的结果:


image.png

spring获取request的方法为
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest()

相关文章

网友评论

      本文标题:Spring AOP学习笔记三

      本文链接:https://www.haomeiwen.com/subject/mlwybftx.html