Spring AOP从原理到源码(二)
2020-04-20 本文已影响0人
李不言被占用了
接着上一节:Spring AOP从原理到源码(一)
手写一个简单AOP
- 配置
Interceptor
- 创建代理对象
- 调用过程封装调用链,然后逐个调用
解剖AOP,从简单到复杂
再次重复,要完成一个AOP,核心就两部分:
- 代理对象的创建
- 调用过程的拦截
为了完成一个简单的AOP组件,我们大致需要以下一些组件: - 用于创建代理对象的工厂;
- 用于动态拦截方法的拦截器;
- 推荐拦截器链执行的
MethodInvocation
(叫方法调用者?)。
1. 方法拦截器
抽象类:
public abstract class MethodBeforeInterceptor implements MethodInterceptor {
/**
* 在方法调用前执行
* @param method
* @param args
* @param target
* @throws Throwable
*/
public abstract void before(Method method, Object[] args, Object target) throws Throwable;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// 先调用before方法
this.before(invocation.getMethod(), invocation.getArguments(), invocation.getThis());
// 继续执行调用链
return invocation.proceed();
}
}
public abstract class AfterReturningInteceptor implements MethodInterceptor {
public abstract void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// 先执行目标方法获取到返回值
Object retVal = invocation.proceed();
this.afterReturning(retVal, invocation.getMethod(), invocation.getArguments(), invocation.getThis());
// 返回
return retVal;
}
}
具体实现:
public class FirstMethodBeforeInteceptor extends MethodBeforeInterceptor {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(String.format("这是FirstMethodBeforeInteceptor#before,正在拦截%s方法", method.getName()));
}
}
public class FirstAfterReturningInterceptor extends AfterReturningInteceptor {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(String.format("这是FirstAfterReturningInterceptor#afterReturning,正在拦截%s方法,它的返回值是%s", method.getName(), returnValue));
}
}
2. MethodInvocation
public class ProxyMethodInvocation implements MethodInvocation {
private Method method;
private Object[] args;
private Object target;
private List<MethodInterceptor> interceptorList;
int currentIndex = -1;
public ProxyMethodInvocation(Method method, Object[] args, Object target, List<MethodInterceptor> interceptorList) {
this.method = method;
this.args = args;
this.target = target;
this.interceptorList = interceptorList;
}
@Override
public Method getMethod() {
return this.method;
}
@Override
public Object[] getArguments() {
return this.args;
}
@Override
public Object proceed() throws Throwable {
if (currentIndex == interceptorList.size() -1) {
return method.invoke(target, args);
}
MethodInterceptor nextInteceptor = interceptorList.get(++currentIndex);
return nextInteceptor.invoke(this);
}
@Override
public Object getThis() {
return this.target;
}
@Override
public AccessibleObject getStaticPart() {
return this.method;
}
}
3. 创建代理的工厂
public class ProxyFactory implements InvocationHandler {
// 被代理的对象
private Object target;
// 被代理对象的所有拦截器
List<MethodInterceptor> interceptorList = new ArrayList<>();
public ProxyFactory(Object target) {
this.target = target;
}
public Object getProxy() {
Class<?>[] allInterfaces = ClassUtils.getAllInterfaces(target);
return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), allInterfaces, this);
}
public void addInterceptor(MethodInterceptor interceptor) {
interceptorList.add(interceptor);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
ProxyMethodInvocation invocation = new ProxyMethodInvocation(method, args, target, interceptorList);
return invocation.proceed();
}
}
4. 测试
写一个Service
,作为被代理对象。
public interface IService {
String doService(String msg);
}
public class MyServiceImpl implements IService {
@Override
public String doService(String msg) {
System.out.println("正在执行MyServiceImpl#doService");
return "Hello " + msg;
}
}
以下代码比较简单。
public class ProxyFactoryTest {
@Test
public void test() {
MyServiceImpl target = new MyServiceImpl();
ProxyFactory factory = new ProxyFactory(target);
IService proxy = (IService) factory.getProxy();
Assert.assertTrue(Proxy.isProxyClass(proxy.getClass()));
}
@Test
public void test1() {
MyServiceImpl target = new MyServiceImpl();
ProxyFactory factory = new ProxyFactory(target);
factory.addInterceptor(new FirstMethodBeforeInteceptor());
IService proxy = (IService) factory.getProxy();
proxy.doService("myaop");
}
@Test
public void test2() {
MyServiceImpl target = new MyServiceImpl();
ProxyFactory factory = new ProxyFactory(target);
factory.addInterceptor(new FirstAfterReturningInterceptor());
IService proxy = (IService) factory.getProxy();
proxy.doService("myaop");
}
@Test
public void test3() {
MyServiceImpl target = new MyServiceImpl();
ProxyFactory factory = new ProxyFactory(target);
factory.addInterceptor(new FirstAfterReturningInterceptor());
factory.addInterceptor(new FirstMethodBeforeInteceptor());
IService proxy = (IService) factory.getProxy();
proxy.doService("myaop");
}
@Test
public void test4() {
MyServiceImpl target = new MyServiceImpl();
ProxyFactory factory = new ProxyFactory(target);
factory.addInterceptor(new FirstMethodBeforeInteceptor());
factory.addInterceptor(new SecondMethodBeforeInteceptor());
IService proxy = (IService) factory.getProxy();
proxy.doService("myaop");
}
@Test
public void test5() {
MyServiceImpl target = new MyServiceImpl();
ProxyFactory factory = new ProxyFactory(target);
factory.addInterceptor(new FirstMethodBeforeInteceptor());
factory.addInterceptor(new SecondMethodBeforeInteceptor());
factory.addInterceptor(new FirstAfterReturningInterceptor());
factory.addInterceptor(new SecondAfterReturningInterceptor());
IService proxy = (IService) factory.getProxy();
proxy.doService("myaop");
}
}
简单的例子,表达的是比较核心的一些东西。还有很多东西没有表达出来,如:
- 为什么创建代理的时候spring添加的是
Advisor
而不是直接添加MethodInterceptor
?
答:一个Advisor(准确说是Advice)可以对应多个MethodInterceptor。
参考
DefaultAdvisorAdapterRegistry#getInterceptors
- 如果不是目标对象没有实现任何接口怎么办?如果让
ProxyFactory
更具通用性,支持在任何情况下都能生成代理对象,完成目标对象的生产?
源码
小结
本节模仿Spring AOP的核心过程实现一个简单的AOP,大致上说明了spring aop的核心过程,如果大家能理解的话,对于spring aop的理解也就不难了。后面将从代理对象的创建、方法调用的拦截过程两部分对spring aop的核心ProxyFactory
进行讲解。下一节一起先看看spring aop是怎么创建代理对象的。
转载请说明出处!