Thursday, November 19, 2009

How to get Spring bean factory in Servlets

Problem In regular web application which does not use any sort of web application framework(for whatever reason), and you need reference to bean declared in Spring Application Context. Solution Using dependency pull we can get reference to required beans inside Servlet Steps: 1) Declare bean context listener in web.xml

<param-name> </param>contextConfigLocation</param-name>
<param-value> </param>/WEB-INF/the-servlet.xml, /WEB-INF/the-service.xml</param-value>

2) In servlet init declare

BeanFactory context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());


Post a Comment