SpringMVC容器中Servlet如何调用service层接口
2017-03-12 22:00
417 查看
标题:在SpringMVC容器中Servlet如何调用Service层接口?
直接最简单有效的方法,重写Servlet的Init()方法。代码如下:
1)、 首先新建一个重写Servlet的Init()方法的类继承HttpServlet
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* 描述:Servlet重写Init()方法
* @author WangKun
*/
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.自己的接口方法。
你自己的东西自己写了。
}
}
直接最简单有效的方法,重写Servlet的Init()方法。代码如下:
1)、 首先新建一个重写Servlet的Init()方法的类继承HttpServlet
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* 描述:Servlet重写Init()方法
* @author WangKun
*/
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.自己的接口方法。
你自己的东西自己写了。
}
}
相关文章推荐
- SpringMVC容器中Servlet如何调用service层接口
- SpringMVC容器中Servlet如何调用service层接口
- springMVC框架下如何实现移动端接口调用2
- springMVC框架下如何实现移动端接口调用——代码实例
- 如何让servelt和Filter注入到spring容器或者在servlet中调用spring中的bean
- 在springmvc中非controller类中调用service接口,以获取springmvc容器方法实现
- springMVC框架下如何实现移动端接口调用——流程简介篇
- springMVC框架下如何实现移动端接口调用
- Servlet容器如何同时来处理多个请求
- Servlet容器是如何工作的?
- 如何重写的 MFC 控件容器接口方法在 Visual C++ 中的几个默认行为(转)
- Servlet 调用 Spring 容器的 service
- (转)如何在linux C/C++语言中调用 sqlite 的函数接口来实现对数据库的管理
- 如何使用spring将service层注入到servlet中去(how to use Spring to inject ur service layer into the servlet )
- 如何使用.NET调用新浪微博接口的实例
- spring不同容器之间如何调用
- Servlet 调用 Spring 容器的 service
- java 如何调用com组件接口方法
- Windows phone如何实现json接口的调用
- Inside VCL:接口指针调用函数的时候,如何获得对象指针以完成函数调用?