PDA

View Full Version : Standard injection into Servlet/Filter


jshellman
Oct 11th, 2005, 06:41 AM
I understand how dependency injection gets works for beans configured in Spring, but what is the standard approach to accessing a spring managed bean from a servlet or filter? I'm not using any web framework, just a plan old servlet/filter.

Thanks.

Colin Yates
Oct 11th, 2005, 07:04 AM
If you are using a filter, checkout http://www.springframework.org/docs/api/org/springframework/web/filter/DelegatingFilterProxy.html. It basically lets you define your filter within a spring context.

So I have in web.xml:

<filter>
<filter-name>pageNotFoundFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterPro xy</filter-class>
</filter>


and in my context.xml I have a bean called "pageNotFoundFilter". Very nice :)

With regard to servlets, not sure. Why not just use a controller? If you really must use a servlet, you can get hold of the web application context using: WebApplicationContextUtils.getRequiredWebApplicati onContext(servletContext);

HTH.