I am looking for solution to intercept parent class inherited method which is called from child another method. Parent class LoggerException
class having handleException
method and I am calling this method from its child class SubLoggerException
's method getException
, trying to intercept inherited method handleException
from 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 another instance method from a Spring AOP proxy, these calls are not intercepted. The reason is that the actual execution of an intercepted method always ocurrs at the original bean (the proxy's target bean). As a consequence the other method will neven be called on the proxy object, but always on the target bean itself. What you need is a way to access the proxy from inside the target bean and then call the method on the proxy. You can get the proxy this way:
AopContext.currentProxy()
So, what you have to do is:
public void getException(){
((LoggerException)AopContext.currentProxy).handleException(null);
}
But consider that the proxy must be accessible if this shall work. This can be configured in your appContext.xml:
<aop:aspectj-autoproxy expose-proxy="true"/>
Hope it helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.