【发布时间】:2017-04-02 01:00:18
【问题描述】:
问题标题没有完全描述问题的含义,但我不能在标题限制内做到这一点。如果你帮我把它改得更方便,我会很高兴的。
我有一些如下描述的类层次结构:
抽象父类:
public abstract class AbstractParent {
public void realParentMethod() {
System.out.println("invoking abstractMethod from realParentMethod");
abstractMethod();
}
public abstract void abstractMethod();
}
ChildImpl:
public class ChildImpl extends AbstractParent {
public void abstractMethod() {
System.out.println("abstractMethod implementation is running!");
}
}
我想拦截此层次结构中的每个方法调用并添加一些功能。即使方法是相互调用的,我也想每次都拦截它们。
我使用 cglib Enhancer 和 MethodInterceptor 实现来做到这一点:
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
public class Main {
public static void main(String[] srgs) {
AbstractParent parentImpl = new ChildImpl();
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(AbstractParent.class);
enhancer.setCallback(new LocalMethodInterceptor(parentImpl));
AbstractParent proxyImpl = (AbstractParent) enhancer.create();
// straight calling abstractMethod
proxyImpl.abstractMethod();
System.out.println("-----------------");
// calling realParentMethod will lead to abstractMethod calling
proxyImpl.realParentMethod();
}
static class LocalMethodInterceptor<T> implements MethodInterceptor {
private T origin;
public LocalMethodInterceptor(T origin) {
this.origin = origin;
}
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
try {
System.out.println("intercept method["+method.getName()+"] before execution!");
return method.invoke(origin, args);
} finally {
System.out.println("cleanup after method["+method.getName()+"] execution!");
}
}
}
}
但是问题是任何方法都只被拦截一次: 如果我调用抽象方法 - 它将被拦截。但是层次结构是这样设计的,你永远不必调用它(abstractMethod)。您只需调用 realParentMethod,它就会为您做所有事情,这意味着它会从自身调用 abstractMethod。
在这个例子中,如果你调用realParentMethod,只会拦截realParentMethod,会跳过抽象方法。
以下是此示例的一些控制台输出:
intercept method[abstractMethod] before execution!
abstractMethod implementation is running!
cleanup after method[abstractMethod] execution!
-----------------
intercept method[realParentMethod] before execution!
invoking abstractMethod from realParentMethod
abstractMethod implementation is running!
cleanup after method[realParentMethod] execution!
【问题讨论】:
标签: java interceptor cglib