Spring笔记(4)
集成Web环境
1.步骤
导入Spring-web坐标
<!-- spring-web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
将ApplicationContext.xml的文件名称存入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>
编写Servlet
使用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)的更多相关文章
- Spring笔记02_注解_IOC
目录 Spring笔记02 1. Spring整合连接池 1.1 Spring整合C3P0 1.2 Spring整合DBCP 1.3 最终版 2. 基于注解的IOC配置 2.1 导包 2.2 配置文件 ...
- Spring笔记01_下载_概述_监听器
目录 Spring笔记01 1.Spring介绍 1.1 Spring概述 1.2 Spring好处 1.3 Spring结构体系 1.4 在项目中的架构 1.5 程序的耦合和解耦 2. Spring ...
- Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)
Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...
- Spring笔记:事务管理
Spring笔记:事务管理 事务管理 Spring事务管理是通过SpringAOP去实现的.默认情况下Spring在执行方法抛出异常后,引发事务回顾,当然你可以用拦截器或者配置去改变它们. 这部门内容 ...
- Spring笔记:AOP基础
Spring笔记:AOP基础 AOP 引入AOP 面向对象的开发过程中,我们对软件开发进行抽象.分割成各个模块或对象.例如,我们对API抽象成三个模块,Controller.Service.Comma ...
- Spring:笔记整理(1)——HelloWorld
Spring:笔记整理(1)——HelloWorld 导入JAR包: 核心Jar包 Jar包解释 Spring-core 这个jar 文件包含Spring 框架基本的核心工具类.Spring 其它组件 ...
- Spring笔记:IOC基础
Spring笔记:IOC基础 引入IOC 在Java基础中,我们往往使用常见关键字来完成服务对象的创建.举个例子我们有很多U盘,有金士顿的(KingstonUSBDisk)的.闪迪的(SanUSBDi ...
- Spring笔记(6) - Spring的BeanFactoryPostProcessor探究
一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...
- spring笔记----看书笔记
上周末看了一章以前javaee轻量级的书spring部分,简单做了一些笔记 // ApplicationContext ac=new ClassPathXmlApplicationContext(&q ...
- Spring 笔记(三)Bean 装配
前言 Spring 有两大核心,也就分成两份笔记分别记录. 其一是管理应用中对象之间的协作关系,实现方式是依赖注入(DI),注入依赖的过程也被称为装配(Wiring). 基于 JavaConfig 的 ...
随机推荐
- Java基础——ArrayList方法全解(字典版)
引言 在使用集合 ArrayList 的时候,经常使用add.remove等,其他的没用过,甚至没听说过的还有很多.现在在这个教程中,简单的了解一下,不要求全都记下.相当于在你脑袋里建一个索引,就是有 ...
- Go是一门什么样的语言?
Go语言基本介绍 首先Go语言诞生于2007年由谷歌公司研发,2009年开源,2012年推出1.0版本,Go是一种语言层面支持并发(Go最大的特色.天生支持并发).内置runtime,支持垃圾回收(G ...
- 《手把手教你》系列技巧篇(十四)-java+ selenium自动化测试-元素定位大法之By xpath上卷(详细教程)
1.简介 按宏哥计划,本文继续介绍WebDriver关于元素定位大法,这篇介绍定位倒数二个方法:By xpath.xpath 的定位方法, 非常强大. 使用这种方法几乎可以定位到页面上的任意元素. ...
- 「GM_脚本」获取 GitHub 项目文件的 jsDelivr CDN 地址「好像没啥用系列」
基本信息: name:「 GitHub 」获取文件的 jsDelivr 地址 desc:获取项目文件的 CDN 地址 url: https://github.com/wdssmq/userscript ...
- Bugku-web-md5 collision(NUPT_CTF)
总结了两道MD5绕过的题目. 根据MD5的特性,有两点漏洞 1.两个开头为0的md5值相同. 2.md5不能处理数组. 3.==用法,0 == 字符串是成立的,从而可以绕过MD5检查. 根据特性,我们 ...
- C++ //继承同名静态成员处理方式
1 //继承同名静态成员处理方式 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 6 cl ...
- 【剑指offer】59 - I. 滑动窗口的最大值
剑指 Offer 59 - I. 滑动窗口的最大值 知识点:队列:滑动窗口:单调 题目描述 给定一个数组 nums 和滑动窗口的大小 k,请找出所有滑动窗口里的最大值. 示例 输入: nums = [ ...
- goproxy.io
goproxy.io 是全球最早的 Go modules 镜像代理服务之一, 采用 CDN 加速服务为开发者提供依赖下载, 该服务由一批热爱开源, 热爱 Go 语言的年轻人开发维护.从 Go 1.11 ...
- Longhorn,Kubernetes 云原生分布式块存储
Longhorn 是用于 Kubernetes 的轻量级.可靠且功能强大的分布式块存储系统. Longhorn 使用容器(containers)和微服务(microservices)实现分布式块存储. ...
- SpringBoot开发一
项目介绍 牛客高级项目课,主要是完成牛客网的讨论社区的搭建.项目在github上. 涉及到的技术架构: Spring,SpringBoot,SpringMVC,MyBatis,Redis,Kafka( ...