集成Web环境

1.步骤

  1. 导入Spring-web坐标

    1. <!-- spring-web-->
    2. <dependency>
    3. <groupId>org.springframework</groupId>
    4. <artifactId>spring-web</artifactId>
    5. <version>5.0.5.RELEASE</version>
    6. </dependency>
  2. 将ApplicationContext.xml的文件名称存入web.xml的全局参数中

    1. <!-- 全局初始化参数-->
    2. <!--applicationContext文件名-->
    3. <context-param>
    4. <param-name>contextConfigLocation</param-name>
    5. <param-value>classpath:applicationContext.xml</param-value>
    6. </context-param>
  3. 配置监听器

    1. <!-- 配置监听器-->
    2. <listener>
    3. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    4. </listener>
  4. 编写Servlet

  5. 使用WebApplicationContextUtils获取ApplicationContext(获取的为子类WebApp..)

    1. //获取app对象(使用工具包中的方法)
    2. ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);

2.pom.xml

  1. <!-- servlet-->
  2. <dependency>
  3. <groupId>javax.servlet</groupId>
  4. <artifactId>javax.servlet-api</artifactId>
  5. <version>4.0.1</version>
  6. <scope>provided</scope>
  7. </dependency>
  8. <!-- servlet-jsp-->
  9. <dependency>
  10. <groupId>javax.servlet.jsp</groupId>
  11. <artifactId>javax.servlet.jsp-api</artifactId>
  12. <version>2.2.1</version>
  13. <scope>provided</scope>
  14. </dependency>
  15. <!-- spring-context-->
  16. <dependency>
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-context</artifactId>
  19. <version>5.0.5.RELEASE</version>
  20. </dependency>
  21. <!-- jdbc-druid-->
  22. <dependency>
  23. <groupId>com.alibaba</groupId>
  24. <artifactId>druid</artifactId>
  25. <version>1.0.9</version>
  26. </dependency>
  27. <!-- jdbc-c3p0-->
  28. <dependency>
  29. <groupId>c3p0</groupId>
  30. <artifactId>c3p0</artifactId>
  31. <version>0.9.1.2</version>
  32. </dependency>
  33. <!-- spring-web-->
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-web</artifactId>
  37. <version>5.0.5.RELEASE</version>
  38. </dependency>
  1. <plugin>
  2. <groupId>org.apache.tomcat.maven</groupId>
  3. <artifactId>tomcat7-maven-plugin</artifactId>
  4. <version>2.2</version>
  5. </plugin>

3.servlet具体实现

3.1工具包实现:(封装好的)

  • ​ servlet中的app通过工具包中的方法获得()
  • ​ 在servletContext域中获得app对象
  • ​ 域中app对象在监听器模块内部实现存储
  1. public class ApplicationContextLoaderUtil {
  2. public static ApplicationContext getApplicationContext(ServletContext servletContext){
  3. //在域中获取applicationContext对象
  4. ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
  5. //测试
  6. System.out.println("通过工具类获取app");
  7. return app;
  8. }
  9. }

3.2监听器实现(封装好的)

手动配置(web.xml中)

  1. <!-- 全局初始化参数-->
  2. <!--applicationContext文件名-->
  3. <context-param>
  4. <param-name>contextConfigLocation</param-name>
  5. <param-value>classpath:applicationContext.xml</param-value>
  6. </context-param>
  7. <!-- 配置监听器-->
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>

内部封装好

  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2. //在servletContext中获取ApplicationContext对象
  3. //获取servletContext
  4. ServletContext servletContext = request.getServletContext();
  5. //获取app对象(使用工具包中的方法)
  6. ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  7. //获取userService
  8. UserService userService = app.getBean(UserService.class);
  9. userService.save();

4.注意

配置pom.xml时:

servlet-api和servlet-jsp-api时应该加入范围

  1. <scope>provided</scope>

applicationContext.xml中:

添加扫描注解

  1. <context:component-scan base-package="dao"/>
  2. <context:component-scan base-package="service"/>

Spring笔记(4)的更多相关文章

  1. Spring笔记02_注解_IOC

    目录 Spring笔记02 1. Spring整合连接池 1.1 Spring整合C3P0 1.2 Spring整合DBCP 1.3 最终版 2. 基于注解的IOC配置 2.1 导包 2.2 配置文件 ...

  2. Spring笔记01_下载_概述_监听器

    目录 Spring笔记01 1.Spring介绍 1.1 Spring概述 1.2 Spring好处 1.3 Spring结构体系 1.4 在项目中的架构 1.5 程序的耦合和解耦 2. Spring ...

  3. Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)

    Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...

  4. Spring笔记:事务管理

    Spring笔记:事务管理 事务管理 Spring事务管理是通过SpringAOP去实现的.默认情况下Spring在执行方法抛出异常后,引发事务回顾,当然你可以用拦截器或者配置去改变它们. 这部门内容 ...

  5. Spring笔记:AOP基础

    Spring笔记:AOP基础 AOP 引入AOP 面向对象的开发过程中,我们对软件开发进行抽象.分割成各个模块或对象.例如,我们对API抽象成三个模块,Controller.Service.Comma ...

  6. Spring:笔记整理(1)——HelloWorld

    Spring:笔记整理(1)——HelloWorld 导入JAR包: 核心Jar包 Jar包解释 Spring-core 这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件 ...

  7. Spring笔记:IOC基础

    Spring笔记:IOC基础 引入IOC 在Java基础中,我们往往使用常见关键字来完成服务对象的创建.举个例子我们有很多U盘,有金士顿的(KingstonUSBDisk)的.闪迪的(SanUSBDi ...

  8. Spring笔记(6) - Spring的BeanFactoryPostProcessor探究

    一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...

  9. spring笔记----看书笔记

    上周末看了一章以前javaee轻量级的书spring部分,简单做了一些笔记 // ApplicationContext ac=new ClassPathXmlApplicationContext(&q ...

  10. Spring 笔记(三)Bean 装配

    前言 Spring 有两大核心,也就分成两份笔记分别记录. 其一是管理应用中对象之间的协作关系,实现方式是依赖注入(DI),注入依赖的过程也被称为装配(Wiring). 基于 JavaConfig 的 ...

随机推荐

  1. 如何反编译Python写的exe到py

    参考链接: https://blog.csdn.net/qq_44198436/article/details/97314626?depth_1-utm_source=distribute.pc_re ...

  2. spring.jpa.open-view问题

    由ReentrantLock和JPA(spring.jpa.open-in-view)导致的死锁问题原因分析. 问题 在压测过程中,发现服务经过一段时间压测之后出现无响应,且无法自动恢复. 分析 从上 ...

  3. Python - 赋值运算符

    前置知识 先了解下变量: https://www.cnblogs.com/poloyy/p/15042257.html 再了解下算术运算符: https://www.cnblogs.com/poloy ...

  4. tomcat与springmvc 结合 之---第20篇 springmvc 对于加载的bean对象存储在哪里

  5. 解决跨网段intouch嵌入视频问题

    在自控项目中,一般会将视频网络和自控网络分开,分属于两个不同的逻辑网段,以避免局域网ip不够用的问题.这就造成了一个问题,如何实现在自控网络上位机访问摄像头并嵌入使用?这里其实很简单,因为这两个网络本 ...

  6. JBoss 5.x/6.x 反序列化漏洞(CVE-2017-12149)

    检测

  7. 2020年度钻石C++C学习笔记(2)--《博学谷》

    2020年度钻石C++C--<博学谷> 1.以下标示符中命名合法的是A A.__A__ B.ab.c C.@rp D.2Y_ 2.设 a 和 b 均为 double 型变量,且a=5.5. ...

  8. Windows协议 LDAP篇 - 域权限

    windows 访问控制模型 也就是大名鼎鼎的ACM,access control mode 由两部分组成的. 访问令牌(access tokens) 其中包含有关登录用户的信息(User SID,G ...

  9. MySQL:获取元数据

    元数据就是描述数据的数据,在很多时候我们都需要查询元数据 比如:想知道数据库有多少个表,表里面有哪些字段,数据表是什么时候创建的.在什么时候更新过等等 使用SQL注入的时候也得获取数据库的元数据才能进 ...

  10. MyBatis学习04(注解开发)

    7.使用注解开发 7.1 面向接口编程 根本原因 : 解耦 , 可拓展 , 提高复用 , 分层开发中 , 上层不用管具体的实现 , 大家都遵守共同的标准 , 使得开发变得容易 , 规范性更好 在一个面 ...