forked from coderbruis/JavaSourceCodeLearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAspectJTest.java
More file actions
68 lines (60 loc) · 1.58 KB
/
Copy pathAspectJTest.java
File metadata and controls
68 lines (60 loc) · 1.58 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.bruis.learnaop.testaspectJ;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class AspectJTest {
/**
* 环绕通知中,在JoinPoint.process()方法前调用的方法,会在@Before调用之前调用;
* 在JoinPoint.process()方法后调用的方法,会在@After调用之前调用;
* 如下:
* @AroundBefore the testaspectJ()...
* Before the testaspectJ()...
* testaspectJ()...
* @AroundAfter the testaspectJ()...
* After the testaspectJ()...
*
* 为啥书上的版本是:
* Before the testaspectJ()...
* @AroundBefore the testaspectJ()...
* testaspectJ()...
* After the testaspectJ()...
* @AroundAfter the testaspectJ()...
*/
/**
* 定义的切点
*/
@Pointcut("execution(* *.test(..))")
public void test(){}
/**
* 前置通知
*/
@Before("test()")
public void beforeTest() {
System.out.println("beforeTest()...");
}
/**
* 后置通知
*/
@After("test()")
public void after() {
System.out.println("afterTest()...");
}
/**
* 环绕通知
* @param p
* @return
*/
@Around("test()")
public Object aroundTest(ProceedingJoinPoint p) {
System.err.println("beforeTest by @Around...");
Object o = null;
try {
// 处理
o = p.proceed();
} catch (Throwable a) {
a.printStackTrace();
}
System.err.println("afterTest by @Around...");
return o;
}
}