在web项目中集成Spring

一、使用Servlet进行集成测试

1.直接在Servlet 加载Spring 配置文件

  1. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  2. HelloService helloService = (HelloService) applicationContext.getBean("helloService");
  3. helloService.sayHello();
 

问题: 每次请求都会加载Spring环境,初始化所有Bean ,性能问题 !!!

解决方案一: 将代码放入Servlet init 中 , 无法保证所有Servlet都能使用 ApplicationContext

解决方案二: ServletContext,在tomcat启动时, 加载Spring配置文件,获得对象 ,放入ServletContext

* ServletContextListener

2.导入spring-web.jar

保存 ContextLoaderListener 完成在Servlet初始化阶段,加载Spring配置文件,将工厂对象放入 ServletContext

3.配置web.xml

  1. <listener>
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>
* 默认读取 WEB-INF/applicationContext.xml 

配置全局参数 contextConfigLocation 指定 配置文件位置

  1. <context-param>
  2.     <param-name>contextConfigLocation</param-name>
  3.     <param-value>classpath:applicationContext.xml</param-value>
  4. </context-param>
 

4.修改Servlet代码

从ServletContext中获得 Spring工厂

第一种:

  1. WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
 

第二种:

  1. WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
 
**********************************************************************************完整代码*********************************************************************************
1)编写service(bean):
  1. publicclassHelloService{
  2. publicvoid sayHello(){
  3. System.out.println("hello,Spring web");
  4. }
  5. }
2)注册bean:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <beanid="helloService"class="cn.itcast.service.HelloService"></bean>
  7. </beans>
3)配置web.xml文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-appversion="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <!-- 配置Spring 监听器 -->
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>
  11. <!-- 配置Spring配置文件所在位置 -->
  12. <context-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>classpath:applicationContext.xml</param-value>
  15. </context-param>
  16. <display-name></display-name>
  17. <servlet>
  18. <servlet-name>HelloServlet</servlet-name>
  19. <servlet-class>cn.itcast.servlet.HelloServlet</servlet-class>
  20. </servlet>
  21. <servlet-mapping>
  22. <servlet-name>HelloServlet</servlet-name>
  23. <url-pattern>/hello</url-pattern>
  24. </servlet-mapping>
  25. <welcome-file-list>
  26. <welcome-file>index.jsp</welcome-file>
  27. </welcome-file-list>
  28. </web-app>
4)编写servlet(测试)
  1. publicclassHelloServletextendsHttpServlet{
  2. publicvoid doGet(HttpServletRequest request,HttpServletResponse response)
  3. throwsServletException,IOException{
  4. WebApplicationContext applicationContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  5. HelloService helloService =(HelloService) applicationContext.getBean("helloService");
  6. helloService.sayHello();
  7. }
  8. publicvoid doPost(HttpServletRequest request,HttpServletResponse response)
  9. throwsServletException,IOException{
  10. doGet(request, response);
  11. }
  12. }
 

二、Spring 整合 junit4 测试

1、 导入spring-test.jar

2、 编写测试用例

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = "classpath:applicationContext.xml") // 指定配置文件位置
  3. public class HelloServiceTest {
  4.     @Autowired
  5.     private HelloService helloService; // 注入需要测试对象
  6.     @Test
  7.     // 测试
  8.     public void demo2() {
  9.         helloService.sayHello(); // 调用测试方法
  10.     }
  11. }
 

06_在web项目中集成Spring的更多相关文章

  1. Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问

    本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...

  2. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  3. 如何在web项目中配置Spring的Ioc容器

    在web项目中配置Spring的Ioc容器其实就是创建web应用的上下文(WebApplicationContext) 自定义要使用的IoC容器而不使用默认的XmlApplicationContext ...

  4. Axis2在Web项目中整合Spring

    一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...

  5. web项目中获取spring的bean对象

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...

  6. 如何在Web项目中配置Spring MVC

    要使用Spring MVC需要在Web项目配置文件中web.xml中配置Spring MVC的前端控制器DispatchServlet <servlet> <servlet-name ...

  7. 在普通WEB项目中使用Spring

    Spring是一个对象容器,帮助我们管理项目中的对象,那么在web项目中哪些对象应该交给Spring管理呢? 项目中涉及的对象 ​ 我们回顾一下WEB项目中涉及的对象 Servlet Request ...

  8. Java Web学习系列——Maven Web项目中集成使用Spring

    参考Java Web学习系列——创建基于Maven的Web项目一文,创建一个名为LockMIS的Maven Web项目. 添加依赖Jar包 推荐在http://mvnrepository.com/.h ...

  9. java web项目中引入spring

    自己动手实践了一次,发生中间出了一下问题,现整理出来,供参考. Step1: 新建一个java web项目 Step2:下载spring的jar包http://repo.spring.io/libs- ...

随机推荐

  1. SQL SERVER2012秘钥

    来自网络: MICROSOFT SQL SERVER 2012 DEVELOPER 版(开发版)序列号:YQWTX-G8T4R-QW4XX-BVH62-GP68YMICROSOFT SQL SERVE ...

  2. Qt可执行程序写入版本信息

    [1]新建Qt工程 1.1 具体新建步骤不赘述. 1.2 新建工程后文件目录如下: 1.3 留意对比一下你的代码目录,可以发现我的文件目录中多了一个rc类型的资源文件.那么,它也就是关键点. 1.4 ...

  3. android 应用架构随笔四(View、ViewGroup)

    View表示了用户界面的基本构建模块. 一个View占用了屏幕上的一个矩形区域并且负责界面绘制和事件处理.手机屏幕上所有看得见摸得着的都是View. Activity是四大组件中唯一一个用来和用户进行 ...

  4. JSP注意点

    一.JSP页面会编译成一个Servlet类,每个Servlet在容器中只有一个实例:在JSP中声明的变量是成员变量,成员变量只在创建实例时初始化,该变量的值将一直保存,直到实例销毁: 二.输出表达式& ...

  5. 【Pro ASP.NET MVC 3 Framework】.学习笔记.6.SportsStore:导航

    在之前的章节,偶们设置了核心的基础设施,现在我们将使用基础设计添加关键特性,你将会看到投资是如何回报的.我们能够很简单很容易地添加重要的面向客户的特性.沿途,你也会看到一些MVC框架提供的附加的特性. ...

  6. Centos7下使用ELK(Elasticsearch + Logstash + Kibana)搭建日志集中分析平台

    日志监控和分析在保障业务稳定运行时,起到了很重要的作用,不过一般情况下日志都分散在各个生产服务器,且开发人员无法登陆生产服务器,这时候就需要一个集中式的日志收集装置,对日志中的关键字进行监控,触发异常 ...

  7. 浏览器的不兼容,归纳几点html编码要素

    1.文字本身的大小不兼容.同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff下实际占高17px,上留白1px,下留白3px,op ...

  8. PHP高级架构技术年度大盘点

    2015年1月的最后一个周末,上海的冬季虽不如北方的雪窖冰天,但腊月的寒风也足以让人猫 在家中不愿出门.可是,在华美达酒店的一个会议室中,却人声鼎沸.春意融融,第三期商派技术沙龙正在火热进行,本期沙龙 ...

  9. java 编程时候的性能调优

    一.避免在循环条件中使用复杂表达式 在不做编译优化的情况下,在循环中,循环条件会被反复计算,如果不使用复杂表达式,而使循环条件值不变的话,程序将会运行的更快. 例子: import java.util ...

  10. ACM题目————The partial sum problem

    描述 One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choo ...