Announcement Announcement Module
No announcement yet.
Intercept only one implementation of an interface method? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Intercept only one implementation of an interface method?

    Hi All,

    I have an interface with two different implementations. I want to apply some advice to only one of those implementations. I'm using the RegexpMethodPointcutAdvisor. However if I set up the regexp so that it matches the implementation method's signature, the advice never gets called. And obviously setting up the regexp to match the interface method's signature is not the behaviour I want, as it will match the other implementation as well.

    Is there any way I can make spring AOP match only a particular implementation of an interface method? I'm using spring 1.2RC1. Here's an excerpt of my config:

        <bean id="connectionAdvice" class="connection.ConnectionInterceptor">
        <bean id="connectionAdvisor" class="">
            <property name="advice">
                <ref bean="connectionAdvice"/>
            <property name="patterns">
                    <!-- Regexp to match the interface method sig - works, but also matches unwanted implementations -->
                    <!-- Regexp to match the implementation method sig - doesn't work -->

  • #2
    Your best option is probably to implement a custom pointcut with a MethodMatch.matches(Method m, Class targetClass) that checks for the target class you want.



    • #3
      Thanks Rob,

      I'll give it a go. I guess another option is to have the implementation that I want to advise simply delegate to another bean and apply the advice to this other bean instead. Not really very clean though.

      Is there any particular reason why you can't match on the actual implementation method signature as well as the interface method signature? It makes sense to me, but maybe I'm missing something.

      I must admit I'm a bit unclear on how I should be defining my pointcuts when it comes to interface vs implementation. What is the general rule? Should I specify the implementation method signature, or the interface method signature? Or both? What exactly will Spring try to match on? What about when using JDK 1.5 annotations (eg: @Transactional)? Should I annotate the interface method, or the implementation of that method?