Announcement Announcement Module
No announcement yet.
Spring AOP with CGLIB proxies, matching methods on super-types Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring AOP with CGLIB proxies, matching methods on super-types


    I've scanned the Spring 2.5.1 and AspectJ docs on this, but I can't find the best solution. It may well be staring me in the face ...

    The question is - when using Spring AOP with CGLIB2 proxies, can I declare a pointcut to match all methods called only on a particular concrete class, including methods that are actually declared in that classes' super-types?

    Example - I have two concrete classes com.mypackage.MySubClass extends com.mypackage.MyBaseClass, and MyBaseClass declares a method methodInBaseClass(). I want a pointcut on execution of any method in MySubClass, including methodInBaseClass(). I do _not_ want the pointcut to match execution of methodInBaseClass() in MyBaseClass or subclasses other than MySubType.

    If I declare:

    <aop:pointcut id="anySubClassMethod"
            expression="execution(* com.mypackage.MySubClass.*(..))" />
    ... I don't seem to generate advice on execution of methodInBaseClass() in MySubClass. Switching to matching MyBaseClass will provide advice on MySubClass#methodInBaseClass(), but also generates unwanted matches in the base class and other subtypes.

    I'm guessing that I could achieve this by using @AspectJ notation and combining expressions (any thoughts on the best expression?), but if possible, I'd like to stick with Spring XML AOP configuration.

    I realise that this might be a stretch, and that proxying is normally by interfaces, but - is this possible?

    Last edited by brett_s_r; Feb 5th, 2008, 03:00 PM.

  • #2
    The following might be a possibility:

    <aop:pointcut id="anySubClassMethod"
            expression="execution(* com.mypackage.MyBaseClass+.*(..)) and bean(mysubclassbeanname)" />
    Where mysubclassbeanname is the name of a specific bean instance in your application context.

    Another possibility, depending on your actual class structure, might be something like:

    <aop:pointcut id="anySubClassMethod"
            expression="execution(* com.mypackage.MyBaseClass+.*(..)) and target(com.mypackage.MySubClass)" />


    • #3
      Thank you Mike - these are exactly what I need.

      It turns out that upgrading to Spring 2.5.1 was a key for me - I am working within an application which is on 2.0.3.

      For the bean() pointcut, 2.5.x is required, and I'm pretty sure that there was a fix for the CGLIB proxy somewhere between 2.0.3 and 2.0.8, as they behave differently when locating pointcuts on super-type classes with CGLIB proxies.

      Thanks again - all working now.