Configurating Spring Ioc with Servlets

前端 未结 3 1920
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-20 22:02

I\'m new in Spring and want to connect spring ioc into my small(test) web-app.

I have such Servlet ProductServlet:

public class Product         


        
3条回答
  •  北海茫月
    2021-01-20 22:24

    Here is a solution, which might help you:

    public class ProductServlet extends HttpServlet
    {
        private static final long serialVersionUID = 1L;
        private RequestHelper requestHelper = null;
    
        private requestHelperInit(HttpServletRequest request)
        {
           if(requestHelper == null)
           {
              ApplicationContext ap = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
              requestHelper = ap.getBean(RequestHelper.class);
           }
        }
    }
    

    Then call requestHelperInit(request) method in either your doGet() or doPost() method as the first statement.

    If you are still looking for a solution, then I hope this will help you out.

提交回复
热议问题