Configurating Spring Ioc with Servlets

java, servlets, spring

Solution

In your question you have

<bean name="ProductServlet" class="com.epam.productshop.controller.ProductServlet" scope="singleton">
    <property name="requestHelper" ref="requestHelper"/>        
</bean>

You cannot instantiate servlets with Spring container, they are instantiated by servlet container. You're merely declaring another instance of ProductServlet.

So, when the Servlet `init()` method is called you should call

SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, getServletContext());`

To inject the requestHelper declare an `@Autowired` annotated field or property in your servlet:

private RequestHelper requestHelper;

@Autowired
public void setRequestHelper(RequestHelper requestHelper){
  this.requestHelper = requestHelper;
}

from processInjectionBasedOnServletContext javadoc:

Process @Autowired injection for the given target object, based on the current root web application context as stored in the ServletContext.

Problem

I'm new in Spring and want to connect spring ioc into my small(test) web-app. I have such Servlet `ProductServlet`: ``` public class ProductServlet extends HttpServlet{ private static final long serialVersionUID = 1L; private RequestHelper requestHelper; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request); } private void processRequest(HttpServletRequest request){ requestHelper.process(request); } public RequestHelper getRequestHelper() { return requestHelper; } public void setRequestHelper(RequestHelper requestHelper) { this.requestHelper = requestHelper; } } ``` and my web.xml: ``` <servlet> <servlet-name>ProductServlet</servlet-name> <servlet-class>com.epam.productshop.controller.ProductShop</servlet-class> </servlet> <servlet-mapping> <servlet-name>ProductServlet</servlet-name> <url-pattern>/ProductServlet</url-pattern> </servlet-mapping> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring-config.xml </param-value> </context-param> ``` and also I have such spring configuration xml: ``` <bean id="factory" class="com.epam.productshop.readerfactory.ReaderFactory"> <property name="file" value="/xml/products.xml" /> </bean> <bean id="requestHelper" class="com.epam.productshop.requesthelper.RequestHelper" scope="singleton"> <property name="factory" ref="factory" /> </bean> <bean name="ProductServlet" class="com.epam.productshop.controller.ProductServlet" scope="singleton"> <property name="requestHelper" ref="requestHelper"/> </bean> ``` and I have such problem: I want spring set requestHelper object into my servlet during servlet init(). but instead of this it's gives me nullpointer. I'm trying to implement my servlet from `HttpRequestHandler`, writing `SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, getServletContext());` into init() method and other things that i see in the internet but all this things doesn't solve my problem. Please help me

Original source

Related problems