Results 1 to 5 of 5

Thread: Intercepting specific Exception subclass

  1. #1
    Join Date
    Oct 2011
    Posts
    7

    Unhappy 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2011
    Posts
    7

    Default

    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.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Dec 2008
    Location
    India
    Posts
    295

    Default

    Code:
    @Aspectpublicclass AfterThrowingExample {  
    @AfterThrowing(    pointcut="execution(* com.xyz.service..*.*(..))", throwing="ex")  
    public void doRecoveryActions(DataAccessException ex) {    // ...  }}
    Try to do like this, this
    Enjoy
    Rohan Chauhan
    ------------------------------------------------------------------------------
    SpringSource Certified Spring 3.0 Professional


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •