链式-AOP

原创
2017/06/01 11:00
阅读数 52

链式-AOP 属于 责任链模式,eg: 我们可以将每个 Proxy 用一根链子串起来,形成一个 Proxy Chain。然后调用这个 Proxy Chain,让它去依次调用 Chain 中的每个 Proxy。

public interface Proxy { // 具体要串在链子上的东东

    void doProxy(ProxyChain proxyChain);
}
public class ProxyChain { // 链子,用来串Proxy用的

    private List<Proxy> proxyList;
    private int currentProxyIndex = 0;

    private Class<?> targetClass; // 具体哪个类
    private Object targetObject;  // 的哪个对象
    private Method targetMethod;  // 的哪个方法
    private Object[] methodParams; // 有哪几个参数的方法
    private MethodProxy methodProxy;  // 方法代理对象
    private Object methodResult;  // 方法返回结果

    public ProxyChain(Class<?> targetClass, Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy, List<Proxy> proxyList) {
        this.targetClass = targetClass;
        this.targetObject = targetObject;
        this.targetMethod = targetMethod;
        this.methodParams = methodParams;
        this.methodProxy = methodProxy;
        this.proxyList = proxyList;
    }

    public Class<?> getTargetClass() {
        return targetClass;
    }

    public Object getTargetObject() {
        return targetObject;
    }

    public Method getTargetMethod() {
        return targetMethod;
    }

    public Object[] getMethodParams() {
        return methodParams;
    }

    public MethodProxy getMethodProxy() {
        return methodProxy;
    }

    public Object getMethodResult() {
        return methodResult;
    }

    public void doProxyChain() {
        if (currentProxyIndex < proxyList.size()) {
            proxyList.get(currentProxyIndex++).doProxy(this); // 调用链条上的某个Proxy的某个方法
        } else {
            try {
                methodResult = methodProxy.invokeSuper(targetObject, methodParams);
            } catch (Throwable throwable) {
                throw new RuntimeException(throwable);
            }
        }
    }
}
public class ProxyManager { // 一个Proxy工厂,用来创建Proxy用的

    private Class<?> targetClass; // 具体要创建哪一个类的Proxy
    private List<Proxy> proxyList;

    public ProxyManager(Class<?> targetClass, List<Proxy> proxyList) {
        this.targetClass = targetClass;
        this.proxyList = proxyList;
    }

    @SuppressWarnings("unchecked")
    public <T> T createProxy() {
        return (T) Enhancer.create(targetClass, new MethodInterceptor() {
            @Override
            public Object intercept(Object target, Method method, Object[] args, MethodProxy proxy) throws Throwable {
                ProxyChain proxyChain = new ProxyChain(targetClass, target, method, args, proxy, proxyList);
                proxyChain.doProxyChain();
                return proxyChain.getMethodResult();
            }
        });
    }
}

上面的三个类 只是创建了一种链式模式,下面要利用这种链式模式 实现AOP:

// “模版方法模式”,就是定义一个抽象类 里面一些空方法 当模版
public abstract class AbstractProxy implements Proxy { // 实现 Proxy 借口

    @Override
    public final void doProxy(ProxyChain proxyChain) {
        Class<?> cls = proxyChain.getTargetClass(); // 具体哪个类要增强
        Method method = proxyChain.getTargetMethod(); // 的具体哪个方法
        Object[] params = proxyChain.getMethodParams(); // 的方法参数

        begin(); // 所有方法执行前要干什么
        try {
            if (filter(cls, method, params)) {
                before(cls, method, params); // 具体方法执行前要干什么
                proxyChain.doProxyChain(); 
                after(cls, method, params); // 具体方法执行后要干什么
            } else {
                proxyChain.doProxyChain();
            }
        } catch (Throwable e) {
            error(cls, method, params, e);
        } finally {
            end(); // 所有方法执行完后要干什么
        }
    }

    public void begin() {
    }

    public boolean filter(Class<?> cls, Method method, Object[] params) {
        return true;
    }

    public void before(Class<?> cls, Method method, Object[] params) {
    }

    public void after(Class<?> cls, Method method, Object[] params) {
    }

    public void error(Class<?> cls, Method method, Object[] params, Throwable e) {
    }

    public void end() {
    }
}

具体实现:

public class BeforeProxy extends AbstractProxy {

    @Override
    public void before(Class<?> cls, Method method, Object[] params) {
        System.out.println("Before");
    }
}
public class AfterProxy extends AbstractProxy {

    @Override
    public void after(Class<?> cls, Method method, Object[] params) {
        System.out.println("After");
    }
}

编写客户端代码:

public class Client {

    public static void main(String[] args) {
        List<Proxy> proxyList = new ArrayList<Proxy>();
        proxyList.add(new BeforeProxy());
        proxyList.add(new AfterProxy());

        ProxyManager proxyManager = new ProxyManager(GreetingImpl.class, proxyList);
        GreetingImpl greetingProxy = proxyManager.createProxy();

        greetingProxy.sayHello("Jack");
    }
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部