集成Web环境

1.步骤

  1. 导入Spring-web坐标

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

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

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

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

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

2.pom.xml

<!--    servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- servlet-jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- spring-context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- jdbc-druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- jdbc-c3p0-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- spring-web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>

3.servlet具体实现

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

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

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

手动配置(web.xml中)

<!--  全局初始化参数-->
<!--applicationContext文件名-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> <!-- 配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

内部封装好

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

4.注意

配置pom.xml时:

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

<scope>provided</scope>

applicationContext.xml中:

添加扫描注解

<context:component-scan base-package="dao"/>
<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. 前端基础html(二)

    一.html的概念 1.概念:超文本标记语言. 2.超文本,超链接:超级不仅有文本,图片,还有音频,视频等. 3.html:作用:   显示服务器端的响应结果. 二.互联网三大基石 1.url:统一资 ...

  2. Matplotlib不能显示中文和正负号的问题

    参考链接:https://www.jianshu.com/p/240ea3ae0dc9 在使用matplotlib画饼状图时,遇到了如下问题 UserWarning: findfont: Font f ...

  3. Hexo搭建静态博客站点

    什么是Hexo? Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页. 本文将介绍如何在没有域名和云主机的 ...

  4. 构建前端第9篇之(下)---vue3.0将template转化为render的过程

    vue3.0将template转化为render的过程 这里是简单标记下,如何将.vue转换成js文件 具体的,先不研究了,太深,能力有限,达不到呢

  5. 数据结构和算法学习笔记十五:多路查找树(B树)

    一.概念 1.多路查找树(multi-way search tree):所谓多路,即是指每个节点中存储的数据可以是多个,每个节点的子节点数也可以多于两个.使用多路查找树的意义在于有效降低树的深度,从而 ...

  6. 大数据学习(19)—— Flume环境搭建

    系统要求 Java1.8或以上 内存要足够大 硬盘足够大 Agent对源和目的要有读写权限 Flume部署 我这8G内存的电脑之前搭建Hadoop.Hive和HBase已经苟延残喘了,怀疑会卡死,硬着 ...

  7. CF466C 题解

    Description 给定一个数组,求有多少组 \(i,j\) 将整个数组分成和相等的三个数组. Solution 从左往右看一遍,记录每一个 \(i\) 使得 \(\sum_{k=1}^i a_k ...

  8. skywalking简介

    监控的分类 Logging,Metrics和Tracing Logging用于记录离散的事件例如,应用程序的调试信息或错误信息,Logging是我们诊断问题的依据. Metrics用于记录可聚合的数据 ...

  9. Openresty Lua协程调度机制

    写在前面 OpenResty(后面简称:OR)是一个基于Nginx和Lua的高性能Web平台,它内部集成大量的Lua API以及第三方模块,可以利用它快速搭建支持高并发.极具动态性和扩展性的Web应用 ...

  10. 去掉文件中的^M

    一般情况下用:set ff=unix就可以解决问题,如果无效,用下面的方法手工完成: :%s/[ctrl+v] [ctrl+m]//g ctrl+v表示按住Ctrl键再按下v键.