Spring-AOP源码探究
# 动态代理
java自带的动态代理
public class JavaProxyTest {
public static void main(String[] args) {
Cat cat = new Cat();
Animal animal = (Animal) Proxy.newProxyInstance(JavaProxyTest.class.getClassLoader(), new Class[]{Animal.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("前置");
try {
Object o = method.invoke(cat,args);
System.out.println("后置");
return o;
}catch (Exception e){
System.out.println("异常通知");
}
return null;
}
});
System.out.println(animal.getName());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
源码主要在 Proxy
类的静态类 ProxyClassFactory
的apply方法
# aop
public class AopTest {
@Test
public void createProxyByAdvice(){
Cat cat = new Cat();
ProxyFactory proxyFactory = new ProxyFactory(cat);
CountingBeforeAdvice beforeAdvice = new CountingBeforeAdvice();
// advice: 拦截通知行为,所有的方法
proxyFactory.addAdvice(beforeAdvice);
Animal proxy = (Animal) proxyFactory.getProxy();
proxy.getName();
}
@Test
public void createProxyByAdvisor(){
Cat cat = new Cat();
ProxyFactory proxyFactory = new ProxyFactory(cat);
// advice: 拦截通知行为
CountingBeforeAdvice beforeAdvice = new CountingBeforeAdvice();
// advisor: 连接点和通知的适配器
Advisor advisor = new DefaultPointcutAdvisor(beforeAdvice);
proxyFactory.addAdvisor(advisor);
Animal proxy = (Animal) proxyFactory.getProxy();
proxy.getName();
assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
System.out.println(proxy);
}
@Test
public void createProxyByAspectJ(){
Cat cat = new Cat();
ProxyFactory proxyFactory = new ProxyFactory(cat);
// advice: 拦截通知行为
CountingBeforeAdvice beforeAdvice = new CountingBeforeAdvice();
// advisor: 连接点和通知的适配器
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
advisor.setAdvice(beforeAdvice);
advisor.setExpression("execution(* *.getName(..))");
// advice: 切面
proxyFactory.addAdvisor(advisor);
Animal proxy = (Animal) proxyFactory.getProxy();
assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
proxy.getName();
}
}
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
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
# Advice
拦截所有的通知方法
# Advisor
连接点和通知的适配器
# Adviced
包括advice和advisor
@Test
public void createProxyByAdvised(){
Cat cat = new Cat();
ProxyFactory proxyFactory = new ProxyFactory(cat);
// advice: 拦截通知行为
CountingBeforeAdvice beforeAdvice = new CountingBeforeAdvice();
// advisor: 连接点和通知的适配器
AspectJExpressionPointcutAdvisor advisor = new AspectJExpressionPointcutAdvisor();
advisor.setAdvice(beforeAdvice);
advisor.setExpression("execution(* *.getName(..))");
Animal proxy = (Animal) proxyFactory.getProxy();
Advised advised = (Advised) proxy;
advised.addAdvisor(advisor);
assertThat(AopUtils.isJdkDynamicProxy(proxy)).as("Proxy is a JDK proxy").isTrue();
proxy.getName();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19