Announcement Announcement Module
Collapse
No announcement yet.
Intercept only one implementation of an interface method? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
        <bean id="connectionAdvice" class="connection.ConnectionInterceptor">
            ...
        </bean>
    
        <bean id="connectionAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice">
                <ref bean="connectionAdvice"/>
            </property>
            <property name="patterns">
                <list>
                    <!-- Regexp to match the interface method sig - works, but also matches unwanted implementations -->
                    <value>service\.ConnectionMessageService\..*</value>
    
                    <!-- Regexp to match the implementation method sig - doesn't work -->
                    <value>service\.AxsResConnectionMessageService\..*</value>
                </list>
            </property>
        </bean>
    Cheers,
    Charles

  • #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.

    Rgds
    Rod

    Comment


    • #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?

      Thanks,
      Charles

      Comment

      Working...
      X