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- ...
随机推荐
- oracle中dual表的使用
dual表是一个虚拟表,用来和select语句一起使用.1.查看当前用户select user from dual2.用来调用系统函数select to_char(sysdate,'yyyy-mm- ...
- rqnoj-342-最不听话的机器人-dp
dp[i][j][k][[l]: 执行第i步,执行到点(j,k),方向为l时,用的最大步数. 状态转移根据step[i]转移. #include<stdio.h> #include< ...
- 关于 ActiveMQ
今天玩了下 ActiveMQ,希望实现服务器的消息可以通知到各个客户终端. 安装: 1.安装 ActiveMQ 之前必须安装 Java 的 jdk , 可以从此下载: http://www.ora ...
- autohotkey --- win10运行不兼容
在win10下许多脚本运行有问题, 将AutoHotkey.exe设置为兼容模式为win7 同时要设置为以管理员身份运行此程序 这个必须得记录一下.
- android 弹幕效果demo
记得之前有位朋友在我的公众号里问过我,像直播的那种弹幕功能该如何实现?如今直播行业确实是非常火爆啊,大大小小的公司都要涉足一下直播的领域,用斗鱼的话来讲,现在就是千播之战.而弹幕则无疑是直播功能当中最 ...
- jQuery中attr() 和 prop()【转】
Version 1.9.0 开始不建议使用 attr() 来对具有 true 和 false 两个属性的属性进行操作. 具有 true 和 false 两个属性的属性,如 checked, selec ...
- oracle 日期问题
共三部分: 第一部分:oracle sql日期比较: http://www.cnblogs.com/sopost/archive/2011/12/03/2275078.html 第二部分:Oracle ...
- 微信公众平台消息接口API指南
简介 微信公众平台消息接口为开发者提供了一种新的消息处理方式.微信公众平台消息接口为开发者提供与用户进行消息交互的能力.对于成功接入消息接口的微信公众账号,当用户发消息给公众号,微信公众平台服务器会使 ...
- Spring+Quartz实现定时执行任务的配置
1.要想使用Quartz 必须要引入相关的包:以下是我在项目中gradle中的配置: compile 'org.quartz-scheduler:quartz:2.1.1' 2.Scheduler的配 ...
- js 表单操作
order.aspx 订单页- order-detail.aspx订单确认页- 操作:order.aspx提交订单@1,跳转到order-detail.aspx页面,确认页面操作:返回上一步@2- ...