|
| 1 | +# 手写SpringAOP |
| 2 | + |
| 3 | +## 定义advice接口 |
| 4 | + |
| 5 | +```java |
| 6 | +public interface Advice extends InvocationHandler { |
| 7 | +} |
| 8 | +``` |
| 9 | + |
| 10 | +## 定义方法回调接口 |
| 11 | +```java |
| 12 | +public interface MethodInvocation { |
| 13 | + void invoke(); |
| 14 | +} |
| 15 | +``` |
| 16 | + |
| 17 | +## 定义前置通知 |
| 18 | +```java |
| 19 | +public class BeforeAdvice implements Advice { |
| 20 | + |
| 21 | + private Object bean; // bean对象 |
| 22 | + |
| 23 | + private MethodInvocation methodInvocation; // 方法调用 |
| 24 | + |
| 25 | + public BeforeAdvice(Object bean, MethodInvocation methodInvocation) { |
| 26 | + this.bean = bean; |
| 27 | + this.methodInvocation = methodInvocation; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 32 | + // 在目标方法前调用,前置 |
| 33 | + methodInvocation.invoke(); |
| 34 | + return method.invoke(bean, args); |
| 35 | + } |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +## 定义后置通知 |
| 40 | +```java |
| 41 | +public class AfterAdvice implements Advice { |
| 42 | + |
| 43 | + private Object bean; |
| 44 | + |
| 45 | + private MethodInvocation methodInvocation; |
| 46 | + |
| 47 | + public AfterAdvice(Object bean, MethodInvocation methodInvocation) { |
| 48 | + this.bean = bean; |
| 49 | + this.methodInvocation = methodInvocation; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 54 | + // 业务逻辑调用 |
| 55 | + Object invoke = method.invoke(bean, args); |
| 56 | + // 后置通知调用 |
| 57 | + methodInvocation.invoke(); |
| 58 | + return invoke; |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## 定义目标方法接口 |
| 64 | +```java |
| 65 | +public interface HelloService { |
| 66 | + void sayHelloWorld(); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +## 实现目标方法 |
| 71 | +```java |
| 72 | +public class HelloServiceImpl implements HelloService { |
| 73 | + |
| 74 | + @Override |
| 75 | + public void sayHelloWorld() { |
| 76 | + System.out.println("hello world..."); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## SpringAOP的实现 |
| 82 | +```java |
| 83 | +public class SimpleAOP { |
| 84 | + // 反射代理 |
| 85 | + public static Object getProxy(Object bean, Advice advice) { |
| 86 | + return Proxy.newProxyInstance(SimpleAOP.class.getClassLoader() |
| 87 | + , bean.getClass().getInterfaces(), advice); |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## 简单测试 |
| 93 | +```java |
| 94 | +public class SimpleAOPTest { |
| 95 | + public static void main(String[] args) { |
| 96 | + // 1. 创建一个 MethodInvocation 实现类 切面逻辑类 |
| 97 | + MethodInvocation logTask = () -> System.out.println("log task start"); |
| 98 | + MethodInvocation logTaskEnd = () -> System.out.println("log task end"); |
| 99 | + |
| 100 | + // 业务逻辑类 |
| 101 | + HelloServiceImpl helloService = new HelloServiceImpl(); |
| 102 | + |
| 103 | + // 2. 创建一个Advice 切入点 |
| 104 | + BeforeAdvice beforeAdvice = new BeforeAdvice(helloService, logTask); |
| 105 | + AfterAdvice afterAdvice = new AfterAdvice(helloService, logTaskEnd); |
| 106 | + |
| 107 | + // 3. 为目标对象生成代理 |
| 108 | + HelloService helloServiceImplProxy = (HelloService) SimpleAOP.getProxy(helloService, beforeAdvice); |
| 109 | + HelloService helloServiceImplProxyAfter = (HelloService) SimpleAOP.getProxy(helloService, afterAdvice); |
| 110 | + |
| 111 | + helloServiceImplProxy.sayHelloWorld(); |
| 112 | + helloServiceImplProxyAfter.sayHelloWorld(); |
| 113 | + |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
0 commit comments