1
我想知道在调用目标方法后是否可以拦截方法? 例如,你可以看到如下:使用Cglib发布方法调用拦截
@CleanUp
public void doSomething{
...
}
我希望能够在方法调用后拦截方法。 在上面的示例中,我会在调用方法后进行常用清理。
我想知道在调用目标方法后是否可以拦截方法? 例如,你可以看到如下:使用Cglib发布方法调用拦截
@CleanUp
public void doSomething{
...
}
我希望能够在方法调用后拦截方法。 在上面的示例中,我会在调用方法后进行常用清理。
如果您使用标准CGLIB增强器,您可以选择是否要在调用代理方法之前或之后执行代码。例如:
MyClass proxy = (List<String>)Enhancer.create(MyClass.class, new MyInvocationHandler());
proxy.aMethodToInvoke();
.
.
.
class MyInvocationHandler implements MethodInterceptor {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("Before we invoke the method");
Object retObj = proxy.invoke(obj, args);
System.out.println("After we invoke the method");
return retObj;
}
}
,所以,只要后proxy.invoke
通话将代码执行该方法的代理已经调用了返回之后。
是的,你可以使用面向方面的编程。也许从[这里]开始(https://code.google.com/p/google-guice/wiki/AOP)。 –