-
Mar 12th, 2010, 03:59 PM
#1
Spring AOP not working with Autowired Dependencies
I encountered weird problem.
I have defined a Proxy for intercepting all service methods to apply some logic.
<bean id="someAdvice" class="com.something.SomeAdvisor" />
<bean id="someProxy" class="org.springframework.aop.framework.autoproxy .BeanNameAutoProxyCreator" >
<property name="beanNames" value="*Service*,*EJB*"/>
<property name="interceptorNames">
<list>
<value>someAdvice</value>
</list>
</property>
</bean>
SomeAdvisor class:
Class SomeAdvisor implements MethodBeforeAdvice{
public void before(Method method, Object[] args, Object target){
//do something
}
}
I have service methods which are autowired to Controllers:
class someController extends MultiActionController{
@Autowired
@Qualifier("myService1")
SomeSeriviceIF1 someService;
.....
someService.someMethod1();
}
My Service class is :
@Repository("myService1")
public class SomeServiceImpl implements SomeSeriviceIF1 {
someMethod1(){}
}
The problem is:
The method call to someMethod1() is not intercepted when called from controller.
But when I get the WebApplicationContext in my Struts action class and use the getBean() method to get the service and invoke the method someMethod1() then it is intercepted by the proxy defined above.
I am clue less about why the interception happens when bean is received from WebApplicationContext and not when autowired. Please advice.
Thank you in advance.
-
Mar 13th, 2010, 04:49 AM
#2
Use [ code][/code ] tags when posting code
!!!!.
You use annotations, so my guess is you also use context:component-scan in both the ContextLoaderListener and DispatcherServlet. Your auto proxy is probably in the root (ContextLoaderListener) and due to the component-scanning you have 2 instances of the service, 1 proxied, 1 not.
-
Mar 15th, 2010, 12:09 PM
#3
Thank you for your reply.
Yes there were two instances of the service.
I excluded the loading of the servlet.xml during the ContextLoaderListerner execution and let the DispatcherServlet load the servlet.xml file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules