Spring AOP configuration to catch all exceptions

I'm struggling to write / configure a ThrowsAdvice interceptor, which I want to catch all the exceptions created in 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 an implementation of MethodInterceptor, which intercepts certain methods that I want to profile (see how long it takes to execute). Here is the XML configuration file that 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 fine and intercepts exactly when the Main :: doSomething () method is called, so I know I'm on. Using XmlSpy to view the Spring AOP schema, it looks like I can add something like the following to make My ExceptionsInterceptor catch all abandoned exceptions:

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

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

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

+1
java spring xml aop
source share
1 answer

According to the aspectJ method parameter, the examples refer to the @AfterThrowing advice method:

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

and then configuration:

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

Hope this helps.

+2
source share

All Articles