Configurating Spring Ioc with Servlets

前端 未结 3 1922
爱一瞬间的悲伤
爱一瞬间的悲伤 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:45

    In your question you have

    
                
    
    

    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.

提交回复
热议问题