I created a servlet filter which is using some Autowired fields. For making it work I declared it as DelegatingFilterProxy
in web.xml . Before this filter, my enitre spring config was in dispatcher-servlet.xml
but for some reason declaring bean for this filter was not working in dispacher-servlet. So, I declared it in applicationContext.xml
. It started working then but Autwired fields inside filter were then throwing null
. To tackle with it I moved
<context:component-scan base-package="com.myproj.abc" />
to applicationContext, filter started working then but url paths defined by my controller classes are no longer mapped. So I need to pull following two lines also in applicationContext
<mvc:default-servlet-handler />
<mvc:annotation-driven />
This solves the issue. But I was wondering is this the right place for all this code? Because Spring security and for static resources and view mapping all these code goes in dispatcher. In one of my other project I faced same issue and there I did like this, declared only following line in applicationContext
<context:component-scan base-package="com.myproj.abc" />
And in dispatcher-servlet I change component scan package to controller only and kept all other code there only(in dispatcher)
<context:component-scan base-package="com.myproj.abc.controller" />
Could anyone please enlighten me on this confusion.