SpringMVC容器中Servlet如何调用service层接口

   直接最简单有效的方法,重写Servlet的Init()方法。代码以下:java

1)、 首先新建一个重写Servlet的Init()方法的类继承HttpServletweb

import javax.servlet.ServletException;spring

import javax.servlet.http.HttpServlet;this

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;.net

import org.springframework.web.context.WebApplicationContext;继承

import org.springframework.web.context.support.WebApplicationContextUtils;接口

/**get

 * 描述:Servlet重写Init()方法servlet

 * @author WangKunit

 */

public class ServletProxy extends HttpServlet {  

/**

*/

private static final long serialVersionUID = 1L;

public void init() throws ServletException {  

       super.init();  

       WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());  

       AutowireCapableBeanFactory factory = wac.getAutowireCapableBeanFactory();  

       factory.autowireBean(this);  

   }  

}  

2)、新建本身须要的Servlet再继承重写Servlet的类 (ServletProxy) 


import javax.servlet.ServletException;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.springframework.beans.factory.annotation.Autowired;

public class ***Servlet extends ServletProxy {

@Autowired
private ***Service ***Service;


    private static final long serialVersionUID = 2827297299439162553L;  
  
    public void doGet(HttpServletRequest request, HttpServletResponse response)   throws Exception {  
        doPost(request, response);  


    }  
  
  
public void doPost(HttpServletRequest request, HttpServletResponse response)    throws Exception { 

         

          ***Service.本身的接口方法。

 

        你本身的东西本身写了。     }    }

相关文章
相关标签/搜索