java 自定义注解的面向切面的实现(环绕通知)打印方法耗时

原创
03/26 00:02
阅读数 105

代码如下:

1.注解

package com.qimh.aspect;

import java.lang.annotation.*;


/**
 * @Target说明了Annotation所修饰的对象范围
 * @Retention定义了该Annotation被保留的时间长短:
 * @Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API
 * 参考连接:https://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
 */
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodLog {
    String key() default "abc";
    boolean argsWhith() default false;

}

2.切面类

package com.qimh.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @author qiminhui
 */
@Aspect
@Component
public class MethodLogAspect {
    //定义切点
    @Pointcut("@annotation(com.qimh.aspect.MethodLog)")
    public void doAspect() {
    }

    @Around("doAspect()")
    public Object doAround(ProceedingJoinPoint pjd) {
        Object result = null;
        try {
            //拦截的类名
            Class clazz = pjd.getTarget().getClass();
            //拦截的方法
            Method method = ((MethodSignature) pjd.getSignature()).getMethod();
            MethodLog methodLog = method.getAnnotation(MethodLog.class);
            String params = "";
            System.out.println("前置通知");
            long startTime = System.currentTimeMillis();
            if (method != null && methodLog.argsWhith()){
                params = methodLog.key() + " " +Arrays.toString(pjd.getArgs());
            }
            System.out.println("params:" + params);
            result = pjd.proceed();
            System.out.println(clazz.getName() + "->"+method.getName()+" cost:" + (System.currentTimeMillis() - startTime));
            System.out.println("后置通知");
        } catch (Throwable e) {
            System.out.println("异常通知");
        }
        System.out.println("返回通知");
        System.out.println("result:" + result);
        return result;

    }

}

3.控制器

package com.qimh.controller;

import com.qimh.aspect.MethodLog;
import com.qimh.vo.Person;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
/**
 * @author qimh
 */
public class HelloController {

    @RequestMapping("hello")
    @MethodLog(key = "cde",argsWhith = true)
    public String hello(@RequestBody Person person){
        return "hello world";
    }
}

4.vo

package com.qimh.vo;

import com.alibaba.fastjson.JSON;

/**
 * @author qiminhui
 */
public class Person {
    private int age;
    private String name;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}

 

5.控制台

前置通知
params:cde [{"age":10,"name":"张三"}]
com.qimh.controller.HelloController->hello cost:151
后置通知
返回通知
result:hello world

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部