当前位置: 首页 > news >正文

06_在web项目中集成Spring

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. }
 

 



来自为知笔记(Wiz)



转载于:https://www.cnblogs.com/tangwan/p/4674976.html

相关文章:

  • 第四篇 学习OpenCV之访问图像数据
  • 面试毒瘤 之 反转二叉树
  • STM32串口寄存器操作(转)
  • (剑指Offer)面试题41:和为s的连续正数序列
  • html 7.28
  • 每天一个Linux命令—— WC
  • const的作用
  • 重置 Launchpad 和更新APP图标缓存
  • (算法)求1到1亿间的质数或素数
  • java程序设计之完数
  • css 多行显示省略号....
  • python--参数列表的分拆
  • EL表达式从request和session中取值
  • 经典图论500题
  • 下拉列表框实现二级联动
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • Android优雅地处理按钮重复点击
  • Apache的80端口被占用以及访问时报错403
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • extract-text-webpack-plugin用法
  • golang中接口赋值与方法集
  • in typeof instanceof ===这些运算符有什么作用
  • IndexedDB
  • KMP算法及优化
  • Linux中的硬链接与软链接
  • MQ框架的比较
  • 给初学者:JavaScript 中数组操作注意点
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 突破自己的技术思维
  • 在Unity中实现一个简单的消息管理器
  • linux 淘宝开源监控工具tsar
  • Spring Batch JSON 支持
  • ​比特币大跌的 2 个原因
  • "无招胜有招"nbsp;史上最全的互…
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • #QT项目实战(天气预报)
  • (07)Hive——窗口函数详解
  • (delphi11最新学习资料) Object Pascal 学习笔记---第5章第5节(delphi中的指针)
  • (DFS + 剪枝)【洛谷P1731】 [NOI1999] 生日蛋糕
  • (保姆级教程)Mysql中索引、触发器、存储过程、存储函数的概念、作用,以及如何使用索引、存储过程,代码操作演示
  • (附源码)node.js知识分享网站 毕业设计 202038
  • (附源码)spring boot网络空间安全实验教学示范中心网站 毕业设计 111454
  • (附源码)计算机毕业设计ssm基于B_S的汽车售后服务管理系统
  • (三)终结任务
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • **CI中自动类加载的用法总结
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .NET CLR Hosting 简介
  • /dev/VolGroup00/LogVol00:unexpected inconsistency;run fsck manually
  • @EnableConfigurationProperties注解使用
  • [ 隧道技术 ] 反弹shell的集中常见方式(二)bash反弹shell
  • [<MySQL优化总结>]
  • [AI]ChatGPT4 与 ChatGPT3.5 区别有多大
  • [Angularjs]ng-select和ng-options
  • [ArcPy百科]第三节: Geometry信息中的空间参考解析