SpringMVC项目中偶尔用到servlet,如何在servlet中注入service笔记

来源:互联网 发布:淘宝网店怎么改价格 编辑:程序博客网 时间:2024/05/16 07:41

仅此做个笔记,防止注入时而成功时而失败的问题。

package com.zhy.spdb.sso;import java.io.IOException;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.context.support.SpringBeanAutowiringSupport;import com.zhy.spdb.sso.service.IUserService;public class LoginServlet extends HttpServlet{    private static final long serialVersionUID = -5205556322881482237L;    @Autowired    private IUserService userService;    public void init(ServletConfig config) throws ServletException {          //在servlet中注入service        super.init(config);        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());      }       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {            this.doPost(request, response);        }     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{         String username = request.getParameter("username");         String password = request.getParameter("password");         String service = request.getParameter("service");     }}

核心代码

        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext()); 
原创粉丝点击