Spring学习笔记:spring整合web之spring-web架包的引用(WebApplicationContextUtils注入容器)
WebApplicationContextUtils
一、Spring整合web之前
案例:给部门列表添加新部门

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.servlet.annotation.WebServlet; @WebServlet(name = "DeptServlet", urlPatterns = "/DeptServlet")
public class DeptServlet extends javax.servlet.http.HttpServlet {
private static Logger logger = Logger.getLogger(DeptServlet.class);
@Override
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
request.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
String deptname = request.getParameter("name");
int id = Integer.parseInt(request.getParameter("id"));
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IDeptService deptService = (IDeptService) context.getBean("deptService");
Dept dept = new Dept();
dept.setDeptname(deptname);
int i = deptService.addDept(dept);
logger.debug("执行"+i+"条数据!");
logger.debug(action+"\n"+deptname+id);
if (i>0){
request.getRequestDispatcher("success.jsp").forward(request,response);
}else{
response.sendRedirect("index.jsp");
}
}
@Override
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
this.doPost(request, response);
}
}
<html>
<head>
<base href="<%=basePath%>">
<title>添加部门</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta name="Content-Type" content="text/html; charset=utf-8"/>
<%--<link rel="stylesheet" type="text/css" href="style.css">--%>
</head>
<body>
<h2>添加部门</h2>
<form action="/DeptServlet?action=add" method="post">
部门:<input type="text" name="name"/>
序号:<input type="number" name="id"/>
<input type="submit" value="添加"/>
</form>
<h2>修改部门</h2>
<form action="/empServlet?action=modify" method="post">
部门:<input type="text" name="name"/>
部门新名称:<input type="text" name="newname"/>
<input type="submit" value="修改"/>
</form>
</body>
</html>

二、发现问题(资源浪费)

tips:有什么办法可以只创建一次ApplicationContext对象吗?在这里可以使用监听器,在系统启动时创建一个ApplicationContext对象,
之后servlet引用对象即可:::::
三、使用监听器管理ApplicationContext对象(整合Spring)
1.系统添加spring-web整合架包
<!--Web整合-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
2.在web.xml文件中配置监听器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name>
<!--Servlet共享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>
</web-app>
3.Servlet方法中使用WebApplicationContextUtils创建ApplicationContext对象
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
修改案例:
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import javax.servlet.annotation.WebServlet; @WebServlet(name = "DeptServlet", urlPatterns = "/DeptServlet")
public class DeptServlet extends javax.servlet.http.HttpServlet {
private static Logger logger = Logger.getLogger(DeptServlet.class);
@Override
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
request.setCharacterEncoding("UTF-8");
String action = request.getParameter("action");
String deptname = request.getParameter("name");
int id = Integer.parseInt(request.getParameter("id"));
//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
IDeptService deptService = (IDeptService) context.getBean("deptService");
Dept dept = new Dept();
dept.setDeptname(deptname);
int i = deptService.addDept(dept);
logger.debug("执行"+i+"条数据!");
logger.debug(action+"\n"+deptname+id);
if (i>0){
request.getRequestDispatcher("success.jsp").forward(request,response);
}else{
response.sendRedirect("index.jsp");
}
}
@Override
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
this.doPost(request, response);
}
}
使用框架编写程序,也是一个资源整合和优化的过程。
Spring学习笔记:spring整合web之spring-web架包的引用(WebApplicationContextUtils注入容器)的更多相关文章
- Spring学习笔记四 整合SSH
三大框架架构(整合原理) 步骤1:导包 Hibernate包 1.Hibernate包,hibernate/lib/required 2.hibernate/lib/jpa | java persis ...
- Spring学习笔记(1)——初识Spring
一.Spring是什么 通常说的Spring其实指的是Spring Framework,它是Spring下的一个子项目,Spring围绕Spring Framework这个核心项目开发了大 ...
- Spring学习笔记(二) 初探Spring
版权声明 笔记出自<Spring 开发指南>一书. Spring 初探 前面我们简单介绍了 Spring 的基本组件和功能,现在我们来看一个简单示例: Person接口Person接口定义 ...
- Spring学习笔记之整合struts
1.现有项目是通过 <action path="/aaaaAction" type="org.springframework.w ...
- Spring学习笔记之整合hibernate
1.web.xml里边要配置好对应的springxml的路径 <context-param> <param-name>contextConfigLocation</par ...
- Spring 学习笔记之整合Hibernate
Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心SQ ...
- 【Spring学习笔记-1】Myeclipse下Spring环境搭建
*.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...
- Spring 学习笔记(一):Spring 入门
1 Spring简介 Spring是一个轻量级Java开发框架,最早由Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题,是一个分层的Java SE/EE ful ...
- Spring学习笔记之一----基于XML的Spring IOC配置
1. 在spring配置文件中,如果对一个property进行直接赋值,可使用<value>元素,spring负责将值转化为property指定的类型:也可以直接在property元素上使 ...
- Java框架spring 学习笔记(九):Spring的bean管理(@Required、@Component、@Autowired、@Resource注解)
注解:代码里面特殊的标记,使用注解可以完成相关功能 注解写法:@注解名称(属性名.属性值) @Required 用在set方法上,一旦用了这个注解,那么容器在初始化bean的时候必须要进行set,也就 ...
随机推荐
- SQL注入不简单?那是你没有懂它的原理~
我们真的了解SQL注入吗? 不管用什么语言编写的Web应用,它们都用一个共同点,具有交互性并且多数是数据库驱动.在网络中,数据库驱动的Web应用随处可见,由此而存在的SQL注入是影响企业运营且最具破坏 ...
- day 13 课后作业
# -*- coding: utf-8 -*-# @Time : 2019/1/7 18:00# @Author : Endless-cloud# @Site : # @File : day 13 课 ...
- SP9098 LCS3
题目链接 题意分析 \(olinr\) : 序列自动机+一系列的鬼畜操作 相信我 你们没人能切 \(lzxkj\) : \(2^m+vector+\)暴力二分 跑得比你正解还快 首先一看\(m≤5\) ...
- PHP 对目录下所有TXT进行遍历 并正则进行处理 preg_replace
<?php set_time_limit(); //遍历 指定目录下的所有 文件/ $pattern是要匹配的 文件规则 function file_list($dir,$pattern=&qu ...
- zend studio 连PHP自带系统函数 常量都不提示
如果是新建项目,所有PHP文件里面函数都是可以自带提示的. 但是,打开已经建立好的项目时候,貌似无法识别是PHP项目或者其他什么. 此时,在项目上点击: configure->add php s ...
- vue遇见better-scroll
better-scroll better-scroll 是一款重点解决移动端(现已支持 PC 端)各种滚动场景需求的插件.它的核心是借鉴的 iscroll 的实现,它的 API 设计基本兼容 iscr ...
- StackMapTable属性说明
(1)StackMapTable属性的说明 https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.4 (2)S ...
- DOS命令行操作MySQL数据库中文乱码问题解决
我们在 dos 命令行操作中文时,会报错 ’); ERROR (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 原因:因为 ...
- CentOS7服务管理(重启,停止,自动启动命令)
我们对service和chkconfig两个命令都不陌生,systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体. systemctl is-enable ...
- maven项目debug调试不能够进入源码问题解决
Maven项目在debug调试模式的时候,进入调试模式,但是没有进入源码界面. 上述问题的解决方法如下: 第一步: 第二步: 第三步: 第四步: 第五步: 到这里就解决了: