PDA

View Full Version : Apply a MethodInterceptor to one method in an interface?



mlavwilson2
May 31st, 2005, 02:32 PM
...
<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?

tentacle
Jun 1st, 2005, 04:44 AM
You cannot prevent interceptor invocation on every method of the exposed interface. You can however control the invocation:



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;

mlavwilson2
Jun 3rd, 2005, 07:03 AM
<bean id="auditInterceptor" class="...AuditInterceptor" >
<property name="messageSource">
<bean class="org.springframework.context.support.ResourceBundle MessageSource">
<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.RegexpMethodPointc utAdvisor">
<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?

Rod Johnson
Jun 13th, 2005, 06:12 AM
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