Pages

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

<context-param>
<param-name> </param>contextConfigLocation</param-name>
<param-value> </param>/WEB-INF/the-servlet.xml, /WEB-INF/the-service.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


2) In servlet init declare

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


0 comments:

Post a Comment