yujian
2020-03-16 8dcc1ffea99306ebaa9b48fb739f0b627f706d84
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.yeshi.fanli.aspect;
 
import java.io.PrintWriter;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Before;
 
//@Component
//@Aspect
public class TestAspect {
    public static final String EDP = "execution(* com.yeshi.shop.controller.**.*(..))";
 
    @Before(EDP)
    public void testBefore(JoinPoint joinpoint) {
        System.out.println("testBefore");
        System.out.println(joinpoint.getArgs().length);
    }
 
    @After(EDP)
    public void testAfter(JoinPoint joinpoint) {
        System.out.println("testAfter");
        Object[] objs = joinpoint.getArgs();
    }
 
    @AfterReturning(pointcut = EDP, returning = "rvt")
    public void testAfterReturning(JoinPoint joinpoint, Object rvt) {
        System.out.println("testAfterReturning");
    }
 
    @AfterThrowing(throwing = "e", pointcut = EDP)
    public void testAfterThrowing(JoinPoint joinpoint, Exception e) {
        System.out.println("testAfterThrowing");
    }
 
    @Around(EDP)
    public Object testAround(ProceedingJoinPoint joinPoint) {
        
        Object[] args = joinPoint.getArgs();
        PrintWriter print = (PrintWriter) args[1];
        print.print("AOP测试。");
        Object obj = null;
        try {
            // obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return obj;
    }
 
}