0

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.

JimHawkins
  • 4,843
  • 8
  • 35
  • 55
Abhinav
  • 3,322
  • 9
  • 47
  • 63

1 Answers1

5

Some terminology: ServletContext is the class. Servlet context is the Spring ApplicationContext for the DispatcherServlet. Application context, also know as root context is the ApplicationContext loaded by the ContextLoaderListener and stored in the web application's ServletContext. It is therefore available to other web application components.

The servlet context is loaded by the DispatcherServlet. The DispatcherServlet retrieves the application context from the ServletContext and uses it as the parent of the servlet context.

A servlet Filter is web application component that has no relation to the DispatcherServlet, ie. it doesn't know about the servlet context.

The javadoc for DelegatingFilterProxy states

Supports a "targetBeanName" filter init-param in web.xml, specifying the name of the target bean in the Spring application context.

So the Filter bean has to be declared in the application context, not the servlet context.

Put all beans that have application scope in the application context.

Put all beans that have relevance to the MVC stack in the servlet context.

Your component-scan should scan the appropriate packages to support the two rules/suggestions above.

Further reading:

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks it cleared some of my doubts. But where exactly should I put `component-scan` because as I mentioned if I out `component-scan` in `applicationContext` then it does not map my urls of controllers and if I put it in dispatcher then my `Filter` gets deprived of autowiring – Abhinav Mar 09 '14 at 09:22
  • @Abhi Don't think of it as _where do I put my `component-scan`_. Both context might have `component-scan` elements. Follow the guidelines I've set out in my answer. If that means moving your classes to different packages, so be it. Obviously, I can't tell you specifically what to change, you haven't shown us your contexts. Just note that a `component-scan` goes through packages recursively. – Sotirios Delimanolis Mar 09 '14 at 15:50