Spring和Servlet的整合
来源:互联网 发布:剑三新建角色捏脸数据 编辑:程序博客网 时间:2024/05/22 07:47
ApplicationContext是Spring的核心,是”应用的容器”,在Web应用中,我们会用到WebApplicationContext,WebApplicationContext继承自ApplicationContext;WebApplicationContext的初始化方式和BeanFactory.ApplicationContext有所区别,因为WebApplicationContext需要ServletContext实例,也就是说它必须拥有Web容器的前提下才能完成启动的工作。
spring提供了用于启动WebApplicationContext的监听器:org.springframework.web.context.ContextLoaderListener,使用它可以在web应用启动的时候来初始化WebApplicationContext。如果要使用WebApplicationContext则需要从ServletContext取出,Spring提供了一个WebApplicationContextUtils类,可以方便的取出WebApplicationContext,只要把ServletContext传入就可以了。
在Web.xml中配置如下内容:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
如果Spring框架采用B/S系统,通过ServletContext对象获取ApplicationContext对象,然后再通过它获取需要的类实例,常用的两种方法如下:
1、WebApplicationContextwac =
WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext())。
2、WebApplicationContextwac =
WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
方法1:在Servlet在init()方法中从Spring容器中获取bean
publicclass SpringServletextends HttpServlet {
privatestaticfinallongserialVersionUID = 1L;
private WebApplicationContextwac;
StuBizImpl biz;
publicvoid doGet(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException {
biz.show();
}
publicvoid doPost(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException {
doGet(request, response);
}
publicvoid init()throws ServletException {
wac=WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
biz=(StuBizImpl)wac.getBean("stuBizImpl");
}
}
上面的方法虽然简单,但是很显然,Servlet自身没有在Spring的容器的管理之内。
方法二、编写Servlet的代理bean
被代理的Servlet
package cn.com.bochy.servlet;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import cn.com.bochy.biz.StuBizImpl;
@Controller(value="SpringServletProxy")
publicclass SpringServletextends HttpServlet {
privatestaticfinallongserialVersionUID = 1L;
privateWebApplicationContext wac;
@Resource
private StuBizImplbiz;
publicvoid setBiz(StuBizImpl biz) {
this.biz = biz;
}
publicvoid doGet(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException {
biz.show();
}
publicvoid doPost(HttpServletRequest request, HttpServletResponseresponse)
throws ServletException, IOException {
doGet(request, response);
}
}
代理的Servlet
package cn.com.bochy.servlet;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.web.context.WebApplicationContext;
importorg.springframework.web.context.support.WebApplicationContextUtils;
publicclass ServletProxyextends GenericServlet {
private Servletproxy;
private StringservletName;
privatestaticfinallongserialVersionUID = 1L;
@Override
publicvoid init()throws ServletException {
super.init();
WebApplicationContext wac = WebApplicationContextUtils
.getRequiredWebApplicationContext(this.getServletContext());
servletName = getServletName();
System.out.println(servletName);//这里就是web.xml中的<servlet-name>中的属性值,被代理的Servlet的名字和这个属性值保持一致
proxy = (Servlet) wac.getBean(servletName);
}
@Override
publicvoid service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
proxy.service(arg0, arg1);
}
}
Web.xml中的配置信息
<servlet-name>SpringServletProxy</servlet-name>
<servlet-class>cn.com.bochy.servlet.ServletProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpringServletProxy</servlet-name>
<url-pattern>/ServletProxy</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
- Spring和Servlet的整合
- Spring简单整合servlet和mybatis
- xfire整合spring时引入的servlet
- spring和gwt整合,让gwt直接调用spring bean的前端servlet
- spring整合servlet
- Spring整合Servlet
- Spring整合Servlet
- Spring整合Servlet
- Spring 与 Servlet 整合
- 一个和spring整合的依赖注入的servlet,子类可自动装配依赖Bean
- 一个和spring整合的依赖注入的servlet,子类可自动装配依赖Bean
- spring 怎么连接servlet,spring整合servlet,spring管理servlet
- WebWork 和 spring 的整合
- Webwork 和 spring 的整合
- jsf和spring的整合
- hibernate和spring的整合
- dwr和spring的整合
- spring 和hibernate的整合
- c语言排序实例(选择、冒泡、插入、折半、快速)
- 深入剖析Tomcat-实现简单的Web服务器
- Java基础之集合
- 项目采购管理脉络
- 六. NAL Unit格式解析
- Spring和Servlet的整合
- 【LeetCode】数组系列(间隔)
- HEXO搭建个人博客(二):Github上场
- Maven学习总结(5)——聚合与继承
- Simulink仿真时AWGN中的信噪比SNR
- 从mysql查询机构表用java解析成机构树
- 删除指定目录下指定类型的文件
- mybatis的配置二
- 七、 熵编码算法(1)——基础知识