开发者

Spring AOP Configuration for Intercepting All Exceptions

I am struggling to write/configure a ThrowsAdvice interceptor that I want to intercept all exceptions thrown throughout my project:

public class ExceptionsInterceptor implements ThrowsAdvice
{
    public void afterThrowing(final Method p_oMethod, final Object[] p_oArgArray,
        final Object p_oTarget, final Exception p_oException)
    {
        System.out.println("Exception caught by Spring AOP!");
    }
}

I have already successfully configured a MethodInterceptor implementation that intercepts particular methods that I want to profile (see how long it takes them to execute). Here is the XML config file I have so far:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"/>

<bean name="profilingInterceptor" class="org.me.myproject.aop.ProfilingInterceptor"/>

<bean name="exceptionsInterceptor" class="org.me.myproject.aop.ExceptionsInterceptor"/>

<aop:config>
    <aop:advisor advice-ref="profilingInterceptor" pointcut="execution(* org.me.myproject.core.Main.doSomething(..))"/>
</aop:config>

My ProfilingInterceptor works perfectly and intercepts precisely when my 开发者_运维知识库Main::doSomething() method gets invoked - so I known I'm ontrack. Using XmlSpy to look at Spring AOP's schema, it looks like I can add something like the following in order to get my ExceptionsInterceptor to intercept all thrown exceptions:

<aop:aspect>
    <after-throwing method=""/>
</aop:aspect>

However I cannot find any documentation where this is used as an example, and I have no idea how to configure the method attribute so that its a "wildcard" (*) and matches all classes and all methods.

Can anyone point me in the right direction? Thanks in advance!


According to aspectJ examples method parameter refers to @AfterThrowing advice method:

@Aspect
public class LoggingAspect {

  @AfterThrowing(
   pointcut = "execution(* package.addCustomerThrowException(..))",
   throwing= "error")
  public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
    //...
  }
}    

and then the configuration:

 <aop:after-throwing method="logAfterThrowing" throwing="error"   />

Hope it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜