i looking solution intercept parent class inherited method called child method. parent class loggerexception class having handleexception method , calling method child class subloggerexception's method getexception, trying intercept inherited method handleexception aspect programming
public class loggerexception{ public string handleexception(exception genericexception) { system.out.println("enter in loggerexception "); return "success"; } } public class subloggerexception extends loggerexception{ public void getexception(){ handleexception(null); } } @aspect public class errornotificationlogger { private logger logger = logger.getlogger(errornotificationlogger.class); @around("settermethod(o)") public object markedmethodsadvice(proceedingjoinpoint joinpoint, object o) throws throwable { system.out.println(" ****** around advice called ***************** "); return null; } //@pointcut("execution(* com.aop.loggerexception+.handleexception(..)) && target(com.aop.subloggerexception)") //@pointcut("execution(* com.aop.loggerexception+.handleexception(..)) && this(o)") @pointcut("execution(* com.aop.loggerexception.handleexception(..)) && this(o)") public void settermethod(object o) {} } public class app extends abstractservice{ public static void main(string[] args) { applicationcontext appcontext = new classpathxmlapplicationcontext( new string[] { "spring-customer.xml" }); subloggerexception cust = (subloggerexception)appcontext.getbean("subloggerexceptionbean"); system.out.println("*************************"); cust.getexception(); system.out.println("*************************"); try { } catch (exception e) { } } }
when calling instance method spring aop proxy, these calls not intercepted. reason actual execution of intercepted method ocurrs @ original bean (the proxy's target bean). consequence other method neven called on proxy object, on target bean itself. need way access proxy inside target bean , call method on proxy. can proxy way:
aopcontext.currentproxy() so, have is:
public void getexception(){ ((loggerexception)aopcontext.currentproxy).handleexception(null); } but consider proxy must accessible if shall work. can configured in appcontext.xml:
<aop:aspectj-autoproxy expose-proxy="true"/> hope helps!
Comments
Post a Comment