Announcement Announcement Module
Collapse
No announcement yet.
after-throwing aspect not called (for subclasses) Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • after-throwing aspect not called (for subclasses)

    I've run into a case when an after-throwing advice is not executed in certain situations (and no, it does not appear a case of self-invocation). The case is pretty simple. I have an interface, IDAO, definining a bunch of methods like:

    Code:
    public <T> List<T> findAll(final Class<T> persistentClass);
    The interface is implemented in DAOImpl defined as below:

    Code:
    public abstract class DAOImpl extends HibernateDaoSupport implements IDAO {}
    And finally, there are several concrete subclasses that do not override any of the IDAO methods.

    Code:
    class MyDAOImpl extends DAOImpl {}
    There is an aspect defined to catch exceptions:

    Code:
    <aop:aspect id="errorLoggingAspect" ref="ErrorLoggingAdvice">
        <aop:pointcut id="errorLoggingPointcut" expression="execution(* my.package.DAOImpl.*(..))"/>
        <aop:after-throwing pointcut-ref="errorLoggingPointcut" throwing="exception" method="logError"/>
    </aop:aspect>
    and the advice is pretty simple:

    Code:
    public class ErrorLoggingAdvice implements ThrowsAdvice {
    	public void logError(JoinPoint jp, Exception exception) {
                  //logging
    	}
    }

    And here's the problem. Consider the case when at runtime, the concrete DAOImpl is an instance of MyDAOImpl.
    - If the aspect is defined as above, for my.package.DAOImpl, the interception works.
    - If I define the pointcut for my.package.MyDAOImpl then it doesn't.

    However, at runtime,the joinpoint's target is an instance of MyDAOImpl (I checked). Does the after-throwing aspect only work on non-inherited methods?

    Thanks,
    JL

  • #2
    forgot to mention: Spring 2.0.1

    I forgot to version: Spring 2.0.1

    Comment


    • #3
      Originally posted by jeanluc View Post
      And here's the problem. Consider the case when at runtime, the concrete DAOImpl is an instance of MyDAOImpl.
      - If the aspect is defined as above, for my.package.DAOImpl, the interception works.
      - If I define the pointcut for my.package.MyDAOImpl then it doesn't.

      However, at runtime,the joinpoint's target is an instance of MyDAOImpl (I checked). Does the after-throwing aspect only work on non-inherited methods?

      Thanks,
      JL
      are you using jdk proxy or cglib?

      Comment


      • #4
        JDK proxy (Sun's JDK5 to be more precise).

        Comment


        • #5
          Originally posted by jeanluc View Post
          JDK proxy (Sun's JDK5 to be more precise).
          then your pointcut should be on the IDao, not on implemenation classes. just change to IDao+ and it should work fine.

          Comment


          • #6
            Well, it already works with DAOImpl if I want to catch all subclasses with one aspect. What I tried, through, is to have different aspects for different subclasses, hence the attempt to advice only one. Isn't this possible? It shouldn't matter that the implementation of the methods are not defined in MyDAOImpl but in its superclass since all those methods are specified in an interface and should be intercepted by the JDK proxy created by Spring. Am I missing something here?

            Thanks,
            JL

            Comment


            • #7
              You will need the following pointcut:

              Code:
              execution(* my.package.DAOImpl.*(..)) 
              && target(my.package.MyDAOImpl)
              -Ramnivas

              Comment

              Working...
              X