View Full Version : My Interceptor does not intercept!
regisan
Jan 28th, 2005, 05:15 PM
Hello,
I have the following class:
package br.com.ivia.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class HistoryInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation)
throws Throwable {
System.out.println("Bingo!");
Object retVal = methodInvocation.proceed();
return retVal;
}
}
And the following configuration in my WEB-INF\applicationContext.xml:
<beans>
...
<bean id="myBuzzServiceTarget"
class="br.com.ivia.service.impl.BuzzServiceImpl">
<property name="dao">
<ref bean="buzzDAO"/>
</property>
</bean>
<bean id="myHistoryInterceptor"
class="br.com.ivia.aop.HistoryInterceptor" />
<bean id="myBuzzService"
class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="proxyInterfaces">
<value>br.com.ivia.service.BuzzService</value>
</property>
<property name="target">
<ref local="myBuzzServiceTarget" />
</property>
<property name="interceptorNames">
<list>
<value>myHistoryInterceptor</value>
</list>
</property>
</bean>
</beans>
Where BuzzServiceImpl implements the interface BuzzService.
When I started my web app, everything goes right in Tomcat.
Then, when I call any business method from the BuzzService object I don't get any message from myHistoryInterceptor.
What should I do to make the invoke() method from HistoryInterceptor has been executed?
Thanks a lot!
katentim
Jan 29th, 2005, 01:39 AM
This code looks OK at first glance. Can you post more of your code?
To quickly test if the interceptor is working, try writing a test case like:
public static void main(String[] args) {
XmlBeanFactory ctx = new XmlBeanFactory(new ClassPathResource("applicationContext.xml", Test.class));
BuzzService buzzService = (BuzzService) ctx.getBean("myBuzzService");
String result = buzzService.doSomething(10);
}
regisan
Jan 29th, 2005, 06:20 AM
Hello,
Sorry, but... how can I test the AOP Intercptor in a web application?
There is only one configuration for the Spring ContextLoader in web.xml
<web-app ...>
...
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListe ner
</listener-class>
</listener>
...
</web-app>
Have I must added any other configuration in web.xml or applicationContext.xml?
There is another point. I'm using Struts framework too.
Thanks for your help!
(and sorry about my poor English) :-(
regisan
Jan 29th, 2005, 07:06 AM
Hello katentim,
For your information, I did the test using the code that you sent and... it worked!!!
But, it doesn't work when a method from my business object is called from my Struts DispatchAction.
Thanks.
robh
Jan 29th, 2005, 07:24 AM
Regis,
Can you post the code used to inject/or lookup the service in your Struts action please.
Regards,
Rob
regisan
Jan 29th, 2005, 07:39 AM
Hello Rod,
Oops... Injection/lookup? Sorry, but there is only a setService() method for Spring to set the bean.
This is my Action:
package br.com.ivia.web;
import java.util.List;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.validator.DynaValidatorForm;
import br.com.ivia.service.BuzzService;
public class BuzzAction
extends DispatchAction {
private BuzzService service;
public void setService(BuzzService service) {
this.service = service;
}
public ActionForward select(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) {
List buzz = service.selectAll();
servletRequest.setAttribute("buzzList", buzz);
return actionMapping.findForward("showBuzz");
}
...
}
Thanks!
robh
Jan 29th, 2005, 08:00 AM
Regis,
In that code you can check that the service object you are receiving from Spring is the AOP proxy using AopUtils.isAopProxy(). If it is you can cast it to Advised and then query the advisors using getAdvisors() to make sure that your interceptor is in there.
If you are not getting an AOP proxy, make sure you have injected the correct bean into Struts.
See the JavaDocs for Advised and Advisor for more details. You may find it useful to drop in a breakpoint in your Action and check out the service object in the debugger.
Rob
regisan
Jan 29th, 2005, 08:50 AM
Hello Rob,
Yikes! Finally, my Interceptor worked!
The problem was in the action-servlet.xml. I put the correct ref bean for the ProxyFactoryBean.
<!-- action-servlet.xml -->
<beans>
<bean name="/buzz" class="br.com.ivia.web.BuzzAction" >
<property name="service">
<!-- This is a real ProxyFactoryBean -->
<ref bean="myBuzzService"/>
</property>
</bean>
...
</beans>
But, every method from BuzzService will be intercepted. How can I filter the methods that I'm interested to intercept?
Thanks a lot!
erikw
Jan 29th, 2005, 11:43 AM
First add a methodPointcut advisor bean to your context:
<bean id="yourMethodPointcut" class="org.springframework.aop.support.NameMatchMethodPoi ntcutAdvisor">
<property name="advice"><ref bean="myHistoryInterceptor"/></property>
<property name="mappedName"><value>yourMethodName</value></property>
</bean>
Now in your myBuzzService bean, don't have the interceptorNames property point directly to myHistoryInterceptor, instead point to the new "yourMethodPointcut" bean. I think that should work.
Regards,
Erik
regisan
Jan 29th, 2005, 12:55 PM
Hello Erik,
I added the MethodPointcutAdvisor bean as you said, but no method was intercepted by HistoryInterceptor.
<beans>
...
<bean id="myHistoryPointcut" class="org.springframework.aop.support.NameMatchMethodPoi ntcutAdvisor" >
<property name="advice">
<ref bean="myHistoryInterceptor" />
</property>
<property name="mappedNames">
<list>
<value>insert</value>
<value>update</value>
<value>remove</value>
</list>
</property>
</bean>
<bean id="myBuzzService" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="proxyInterfaces">
<list>
<value>br.com.ivia.service.BuzzService</value>
</list>
</property>
<property name="target">
<ref local="myBuzzServiceTarget" />
</property>
<property name="interceptorNames">
<list>
<value>myHistoryPointcut</value>
</list>
</property>
</bean>
</beans>
I tried to use patterns in the mappedNames, like:
...
<property name="mappedNames">
<list>
<value>*.insert*</value>
<value>*.update*</value>
<value>*.remove*</value>
</list>
</property>
...
But didn't get any different result.
I could check the name of the method in HistoryInterceptor, but I think it's so ugly. :?
Thanks for help.[/code]
robh
Jan 30th, 2005, 12:54 PM
Regis,
Pointcuts and their uses are covered in detail in chapter 5 of the Spring reference manual. The information in there should help you to get up and running with this.
Rob
regisan
Jan 31st, 2005, 08:59 AM
Hello Rob,
Ok. I'll check the reference.
I should have done it before. Sorry.
Thanks for help.
Kanika Aggarwal
Jul 15th, 2007, 03:22 AM
Hi
My webContext.xml looks like :
<!-- ========================= BUSINESS DELEGATE DEFINITIONS ========================= -->
<bean id="riDelegate"
class="com.standardchartered.cr.ri.delegate.impl.RIDelega teLocalImpl">
<property name="service" ref="riService" />
</bean>
<bean id="infDelegate"
class="com.standardchartered.cr.inf.delegate.impl.InfDele gateLocalImpl">
<property name="service" ref="infService" />
</bean>
<!-- ========================= STRUTS ACTION DEFINITIONS ========================= -->
<bean id="personAction" scope="prototype"
class="com.standardchartered.cr.ri.action.PersonAction">
<constructor-arg ref="riDelegate" />
<constructor-arg ref="infDelegate" />
</bean>
<!-- ========================= FOR CACHING ========================= -->
<bean id="appCacheManager" class="net.sf.ehcache.CacheManager">
<!-- constructor-arg index="0" type="java.net.URL"
value="classpath:/WEB-INF/ehcache.xml" /-->
</bean>
<aop:config>
<aop:pointcut id="getPersonByCriteriaPointcut"
expression="execution(* *..RIDelegateLocalImpl.getPersonByCriteria())" />
<aop:advisor id="methodCachingAdvisor"
advice-ref="methodCachingAdvice"
pointcut-ref="getPersonByCriteriaPointcut" />
</aop:config>
<bean id="methodCachingAdvice"
class="com.standardchartered.cr.ri.caching.interceptor.Me thodCachingInterceptor">
<property name="cacheManager" ref="appCacheManager" />
</bean>
<!-- =========================CACHING ENDS ========================= -->
the ehcache.xml is:
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache maxElementsInMemory="10" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />
<cache name="getPersonByCriteria" maxElementsInMemory="150" eternal="false"
overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="86400" />
</ehcache>
But my MethodCachingInterceptor class is not getting called.
What could be the problem ? This is pretty urgent, if u can help...
Thanks in advance.....
karldmoore
Jul 15th, 2007, 05:28 AM
I would take a look at the examples in the Spring reference manual, most of the time the issues related to interceptors are either internal method calls, the AOP expression not being correct or you are proxying interfaces and the AOP expression references the class instead.
http://www.springframework.org/docs/reference/aop.html#aop-proxying
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.