forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAOPTest.java
More file actions
29 lines (23 loc) · 1.05 KB
/
SimpleAOPTest.java
File metadata and controls
29 lines (23 loc) · 1.05 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
/**
* @program SpringBooks
* @description: SimpleAOPTest
* @author: mf
* @create: 2020/02/04 20:06
*/
public class SimpleAOPTest {
public static void main(String[] args) {
// 1. 创建一个 MethodInvocation 实现类 切面逻辑类
MethodInvocation logTask = () -> System.out.println("log task start");
MethodInvocation logTaskEnd = () -> System.out.println("log task end");
// 业务逻辑类
HelloServiceImpl helloService = new HelloServiceImpl();
// 2. 创建一个Advice 切入点
BeforeAdvice beforeAdvice = new BeforeAdvice(helloService, logTask);
AfterAdvice afterAdvice = new AfterAdvice(helloService, logTaskEnd);
// 3. 为目标对象生成代理
HelloService helloServiceImplProxy = (HelloService) SimpleAOP.getProxy(helloService, beforeAdvice);
HelloService helloServiceImplProxyAfter = (HelloService) SimpleAOP.getProxy(helloService, afterAdvice);
helloServiceImplProxy.sayHelloWorld();
helloServiceImplProxyAfter.sayHelloWorld();
}
}