Announcement Announcement Module
Collapse
No announcement yet.
Apply a MethodInterceptor to one method in an interface? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Apply a MethodInterceptor to one method in an interface?

    Code:
      ...
      <bean id="auditInterceptor" class="gov.cmfi.console.event.AuditInterceptor" />
    
      <bean id="alarmPanelAdapterTarget" singleton="true" class="gov...">  
        ...
      </bean>
    
      <bean id="alarmPanelAdapter" class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="proxyInterfaces"><value>gov...</value></property>
        <property name="interceptorNames">
          <list>
            <value>auditInterceptor</value>
          </list>
        </property>
        <property name="target"><ref local="alarmPanelAdapterTarget"/></property>
      </bean>
      ...
    I have the above ApplicationContext.xml file. The "alarmPanelAdapter" bean is being intercepted by the "auditInterceptor" as expected. However: How do you apply a filter so that it only gets fired upon a given method, not everyone?

  • #2
    You cannot prevent interceptor invocation on every method of the exposed interface. You can however control the invocation:

    Code:
        public Object invoke&#40;MethodInvocation invocation&#41; throws Throwable &#123;
            if &#40;invocation.getMethod&#40;&#41;.getName&#40;&#41;.equals&#40;"myMethod"&#41;&#41; &#123;
                // do something very particular
            &#125;
            Object rval = invocation.proceed&#40;&#41;;
            return rval;
        &#125;

    Comment


    • #3
      AOP is not right for this problem?

      Code:
        <bean id="auditInterceptor" class="...AuditInterceptor" >
          <property name="messageSource">
            <bean class="org.springframework.context.support.ResourceBundleMessageSource">
              <property name="basename"><value>audit</value></property>
              <property name="useCodeAsDefaultMessage"><value>true</value></property>
            </bean>
          </property>
        </bean>
        
       ...
        
        <bean id="alarmPanelAdapterTarget"           singleton="true" class="...DemoAlarmPanelAdapter">  
          <property name="propability"><value>1</value></property>
        </bean>
      
         <bean id="alarmPanelAdapterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice"><ref local="auditInterceptor"/></property>
            <property name="patterns">
              <list>
                <value>.*.acknowledge</value>
              </list>
            </property>
         </bean>
      This fires the AuditInterceptor only on the acknowledge method, however there is a lot of xml, I would rather see the audit hard coded as on line, than this 10+ lines of xml!

      Any thoughts?

      Comment


      • #4
        Inner beans or autoproxying will make this more concise. However, we do intend to offer simpler XML configuration options in Spring 1.3.

        One advantage of using a pointcut is that it allows Spring to optimize the interceptor--possibly, all interceptors--out of the invocation chain for most methods. This can produce a performance improvement, although usually interception performance on service-layer objects isn't an issue.

        Rgds
        Rod

        Comment

        Working...
        X