Announcement Announcement Module
Collapse
No announcement yet.
Intercepting specific Exception subclass Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Intercepting specific Exception subclass

    I'm trying to configure Spring so that it executes advice when a specific exception subclass (MyTestException) is thrown:

    Code:
        public class MyTestExceptionInterceptor implements ThrowsAdvice
        {
            public void afterThrowing(Method method, Object[] args, Object target, Exception exc)
            {
                // I want this to get executed every time a MyTestException is thrown,
                // regardless of the package/class/method that is throwing it.
            }
        }
    And the XML config:

    Code:
        <bean name="interceptor" class="org.me.myproject.MyTestExceptionInterceptor"/>
    
        <aop:config>
            <aop:advisor advice-ref="interceptor" pointcut="execution(???)"/>
        </aop:config>
    I have a feeling that I should be using the target pointcut specifier (instead of "execution") since - according to the Spring docs (Ch 6) - it seems as though target allows me to specify the type of exception to match against, but I'm not sure if that's wrong, or what my "pointcut" attribute needs to look like.

    I would greatly prefer to keep the AOP config done in XML (as opposed to Java/annotations, but I could probably translate an annotation-based solution into XML if need be.

    Thanks in advance for any suggestions!

  • #2
    I suggest a read of the reference guide especially sections 8.3 which explains schema based AOP. That has even an example of what you are trying to do.

    Comment


    • #3
      Thanks Marten,

      But I already had a gander at the chapter you reference. Those examples only show you how to intercept specific methods whose execution exits by throwing an exception.

      I'm looking for a way to say: In package X, class Y, if Exception subclass Z is thrown, execute advice W.

      After reviewing the play, Spring AOP doesn't seem to support this level of granularity.

      Comment


      • #4
        I'm looking for a way to say: In package X, class Y, if Exception subclass Z is thrown, execute advice W.
        That is simply a pointcut... Nothing more nothing less.


        Code:
        <aop:after-throwing pointcut="execution(* x.y.*(..))" method="handle" throwing="ex" />
        Where ex is the attribute in your handle method on your advice W of the specific type. Nothing more nothing less.

        This is also explained in that chapter, the error you make is that you are trying to utilize the simple aopalliance interceptors with the extensive support in aspectj.

        Comment


        • #5
          Code:
          @Aspectpublicclass AfterThrowingExample {  
          @AfterThrowing(    pointcut="execution(* com.xyz.service..*.*(..))", throwing="ex")  
          public void doRecoveryActions(DataAccessException ex) {    // ...  }}
          Try to do like this, this

          Comment

          Working...
          X