06_在web项目中集成Spring
在web项目中集成Spring
一、使用Servlet进行集成测试
1.直接在Servlet 加载Spring 配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");HelloService helloService = (HelloService) applicationContext.getBean("helloService");helloService.sayHello();
问题: 每次请求都会加载Spring环境,初始化所有Bean ,性能问题 !!!
解决方案一: 将代码放入Servlet init 中 , 无法保证所有Servlet都能使用 ApplicationContext
解决方案二: ServletContext,在tomcat启动时, 加载Spring配置文件,获得对象 ,放入ServletContext
* ServletContextListener
2.导入spring-web.jar
保存 ContextLoaderListener 完成在Servlet初始化阶段,加载Spring配置文件,将工厂对象放入 ServletContext
3.配置web.xml
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
配置全局参数 contextConfigLocation 指定 配置文件位置
<context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param>
4.修改Servlet代码
从ServletContext中获得 Spring工厂
第一种:
WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
第二种:
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
publicclassHelloService{publicvoid sayHello(){System.out.println("hello,Spring web");}}
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><beanid="helloService"class="cn.itcast.service.HelloService"></bean></beans>
<?xml version="1.0" encoding="UTF-8"?><web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><!-- 配置Spring 监听器 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置Spring配置文件所在位置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><display-name></display-name><servlet><servlet-name>HelloServlet</servlet-name><servlet-class>cn.itcast.servlet.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><url-pattern>/hello</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
publicclassHelloServletextendsHttpServlet{publicvoid doGet(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException{WebApplicationContext applicationContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext());HelloService helloService =(HelloService) applicationContext.getBean("helloService");helloService.sayHello();}publicvoid doPost(HttpServletRequest request,HttpServletResponse response)throwsServletException,IOException{doGet(request, response);}}
二、Spring 整合 junit4 测试
1、 导入spring-test.jar
2、 编写测试用例
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = "classpath:applicationContext.xml") // 指定配置文件位置public class HelloServiceTest {@Autowiredprivate HelloService helloService; // 注入需要测试对象@Test// 测试public void demo2() {helloService.sayHello(); // 调用测试方法}}
06_在web项目中集成Spring的更多相关文章
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
- web项目中 集合Spring&使用junit4测试Spring
web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...
- 如何在web项目中配置Spring的Ioc容器
在web项目中配置Spring的Ioc容器其实就是创建web应用的上下文(WebApplicationContext) 自定义要使用的IoC容器而不使用默认的XmlApplicationContext ...
- Axis2在Web项目中整合Spring
一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...
- web项目中获取spring的bean对象
Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...
- 如何在Web项目中配置Spring MVC
要使用Spring MVC需要在Web项目配置文件中web.xml中配置Spring MVC的前端控制器DispatchServlet <servlet> <servlet-name ...
- 在普通WEB项目中使用Spring
Spring是一个对象容器,帮助我们管理项目中的对象,那么在web项目中哪些对象应该交给Spring管理呢? 项目中涉及的对象 我们回顾一下WEB项目中涉及的对象 Servlet Request ...
- Java Web学习系列——Maven Web项目中集成使用Spring
参考Java Web学习系列——创建基于Maven的Web项目一文,创建一个名为LockMIS的Maven Web项目. 添加依赖Jar包 推荐在http://mvnrepository.com/.h ...
- java web项目中引入spring
自己动手实践了一次,发生中间出了一下问题,现整理出来,供参考. Step1: 新建一个java web项目 Step2:下载spring的jar包http://repo.spring.io/libs- ...
随机推荐
- Floyd算法核心代码证明
Flody 大家都知道这个最终模版: for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i ...
- 视频处理控件TVideoGrabber如何重新编码视频/音频(2)
在前面的文中<视频处理控件TVideoGrabber如何重新编码视频>已经讲解了部分TVideoGrabber重新编码音频.视频剪辑的内容,下面将继续说明. 重新编码进程 重新编码开始时, ...
- JQuery ajax方法及参数
©屋主原创,版权归 todayeeee 所有!如需转载,必须在页面明显位置给出原文链接!商业用途请 联系我! $.ajax({ type: 'GET', // 这是请求的方式 可以是GET方 ...
- 基于mjpg_streamer视频服务器移植【转】
本文转载自:http://blog.csdn.net/wavemcu/article/details/7539560 MJPG简介: MJPG是MJPEG的缩写,但是MJPEG还可以表示文件格式扩展名 ...
- android 中 listview 设置自动匹配高度
1.布局文件 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:l ...
- 使用磁盘为Linux添加swap
一.SWAP 说明 1.SWAP 概述 当系统的物理内存不够用的时候,就需要将物理内存中的一部分空间释放出来,以供当前运行的程序使用.那些被释放的空间可能来自一些很长时间没有什么操作的程序,这些被释放 ...
- python-day 1
学python--脚本语言 为了更好的以后,为了更好的自己,加油!!! 1.安装虚拟机如果遇到这样的错误:此主机支持intel vt-x 处于禁用状态错误 解决方法: 进入BIOS后,找到“Syste ...
- 【转】Eclipse插件大全介绍及下载地址
转载地址:http://developer.51cto.com/art/200906/127169.htm 尚未一一验证. eclipse插件大全介绍,以及下载地址 Eclipse及其插件下载网址大全 ...
- StringBuffer中的flush()方法作用
在java API1.6对flush()方法的介绍如下: 方法摘要 void close() 关闭此流,但要先刷新它. void flush() 刷新该流的 ...
- TSP问题
之前写过一道类似的题目,Uva 1347. http://www.cnblogs.com/TreeDream/p/5981535.html 这个题目和TSP问题已经很接近了,只是描述的奇奇怪怪的,从最 ...