binder.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 84 行

JAVA
84
字号
package com.cloudwebsoft.framework.aop;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy;import java.lang.reflect.Method;import java.util.Vector;import java.util.Iterator;import com.cloudwebsoft.framework.aop.base.Advisor;import cn.js.fan.util.ErrMsgException;import java.lang.reflect.InvocationTargetException;public class Binder implements InvocationHandler {    Vector advisors;    public Object proxyObj;    public Binder() {    }    public Object bind(Object obj) {        this.proxyObj = obj;        return Proxy.newProxyInstance(obj.getClass().getClassLoader(),                                      obj.getClass().getInterfaces(), this);    }    public void setAdvisors(Vector advisors) {        this.advisors = advisors;    }    public void Before(Object proxy, Method method, Object[] args) throws            Throwable {        if (advisors==null)            return;        Iterator ir = advisors.iterator();        while (ir.hasNext()) {            Advisor advisor = (Advisor)ir.next();            advisor.Before(proxy, method, args);        }    }    public void After(Object proxy, Method method, Object[] args) throws            Throwable {        if (advisors == null)            return;        Iterator ir = advisors.iterator();        while (ir.hasNext()) {            Advisor advisor = (Advisor) ir.next();            advisor.After(proxy, method, args);        }    }    public void Throw(Object proxy, Method method, Object[] args, Exception e) throws            Throwable {        if (advisors == null)            return;        Iterator ir = advisors.iterator();        while (ir.hasNext()) {            Advisor advisor = (Advisor) ir.next();            advisor.Throw(proxy, method, args, e);        }    }    public Object invoke(Object proxy, Method method, Object[] args) throws            Throwable {        Before(proxy, method, args);        Object result = null;        try {                                                result = method.invoke(proxyObj, args);                     }        catch (InvocationTargetException e) {                        e.printStackTrace();            throw e.getTargetException();                    }        After(proxy, method, args);        return result;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?