spring - How to intercept a method in java -
here method:
public static boolean startmodule(module mod, servletcontext servletcontext, boolean delaycontextrefresh)
here method call in java file:
webmoduleutil.startmodule(module, getservletcontext(), false);
i cannot make changes these files, want intercept method , add of code (i want access parameters well)
code wrote in java file not successful:
public void main(string[] args) throws exception { module module = null; webmoduleutil wmb = new webmoduleutil(); proxyfactory pf = new proxyfactory(wmb); pf.addadvice(new methodinterceptor() { public object invoke(methodinvocation invocation) throws throwable { if (invocation.getmethod().getname().startswith("start")) { system.out.println("method " + invocation.getmethod() + " called on " + invocation.getthis() + " args " + invocation.getarguments()); system.out.println("********************"); object ret = invocation.proceed(); system.out.println("method " + invocation.getmethod() + " returns " + ret); return ret; } return null; } }); webmoduleutil proxy = (webmoduleutil) pf.getproxy(); proxy.startmodule(module, getservletcontext(), false); } private static servletcontext getservletcontext() { // todo auto-generated method stub return null; }
use aop programming. example try read aspectj.
ex code:
import org.aspectj.lang.proceedingjoinpoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.service; @aspect @service public class aspectdemo { @around("aop1()" ) public object intercept(proceedingjoinpoint joinpoint) throws throwable { (object obj : joinpoint.getargs()) { log.debug(obj); } } @pointcut("execution(*my.example.packcage.startmodule.*(..))") public void aop1() { }
Comments
Post a Comment