我对Cglib MethodInterceptor有一些困惑,正如文档中所描述的:所有生成的代理方法都调用这个方法,而不是原始方法。原始方法可以通过使用method对象的普通反射调用,也可以使用MethodProxy (更快)调用。
但是下面的代码有由java.lang.reflect.InvocationTargetException;why?引起的错误任何建议都将不胜感激。
public class CglibInterceptor implements MethodInterceptor {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println(" CglibInterceptor do something");
return method.invoke(obj, args);
}}
发布于 2013-12-02 07:29:30
您发布的代码创建了一个无限循环。使用普通反射和MethodProxy的不同之处在于,MethodProxy将调用被代理类的子类实现,而反射调用将再次被代理。
您可以按如下方式考虑这两个选项:
class NormalClass {
void m() { }
}
class CglibClass extends NormalClass {
void m() {
// This would be like the reflective invocation
this.m();
// This has the same effect as the MethodProxy
super.m();
}
}问题是反射调用是动态绑定的。因此,它们将始终命中代理,而代理只不过是一个动态创建的子类。选择两个调用选项中的一个与速度无关,它关系到您是否想要命中代理。
https://stackoverflow.com/questions/14421421
复制相似问题