简体   繁体   English

寻找使用Spring AOP拦截基类继承方法的解决方案

[英]Looking for solution to intercept baseclass inherited method using Spring AOP

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 父类LoggerException类具有handleException方法,我正在从其子类SubLoggerException的方法getException调用此方法,尝试从方面编程中拦截继承的方法handleException

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. 从Spring AOP代理调用另一个实例方法时,不会拦截这些调用。 The reason is that the actual execution of an intercepted method always ocurrs at the original bean (the proxy's target bean). 原因是被拦截方法的实际执行总是在原始bean(代理的目标bean)处发生。 As a consequence the other method will neven be called on the proxy object, but always on the target bean itself. 结果,将永远不会在代理对象上调用其他方法,但总是在目标bean本身上调用。 What you need is a way to access the proxy from inside the target bean and then call the method on the proxy. 您需要的是一种从目标bean内部访问代理,然后在代理上调用该方法的方法。 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: 可以在您的appContext.xml中进行配置:

 <aop:aspectj-autoproxy expose-proxy="true"/>

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM