简体   繁体   中英

Catch Spring runtime exception with Spring AOP

For logging purposes, we're trying to catch various Spring's runtime exceptions via Spring's own AOP and I must say I've been unsuccessful, so I would appreciate any ideas as to how to approach this.

I've tried something like this:

@Aspect
@Component
public class SomeAspect {

@AfterThrowing(pointcut = "execution(* org.springframwork.oxm..*(..))", throwing = "exception")
@Transactional(propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_COMMITTED)
public void adviseSpringErrorEvents(JoinPoint joinPoint, final UnmarshallingFailureException exception) throws Throwable {

         LOG.debug(this.getClass().getSimpleName() + " - Error message advice fired on method: " + joinPoint.getSignature().getName());
     }
}

The class is autoproxied and the rest of the advice in the aspect class correctly fire, so there has to be a problem with the AspectJ expression.

UPDATE: The code inside the method does not run and it is not my intention to catch the exception in the advice.

Any ideas? Thx in advance.

PS Spring framework version is 3.0.6.

Here are a couple of things that stand out to me:

  1. Your point cut is for the spring package that the exception you are trying to catch it is in, it should be where you are trying to join execution(* com.mypackage..*(..)) .
  2. You are throwing throwable but you do not need to declare anything being thrown. The exception being advised of will not propagate through this code.

Things to try:

  1. Expand your exception parameter to Exception . Maybe the exception you are looking for is in a different hierarchy.
  2. Widen the pointcut to * package level - but probably best not to do both 1 and 2 at the same time.
  3. Increase logging to a more severe level to be sure it is not getting gobbled by your logging framework.

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.

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