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,也就 ...
 
随机推荐
- Java容器中的元素输出
			
1.容器不同于数组,容器若是想输出全部元素,可以直接利用System.out.println(collection) public class TestCollectionArrayPrint { p ...
 - webpack 中文文档
			
webpack 最强最详细中文文档 https://doc.webpack-china.org/guides/getting-started/#- webpack多页应用架构系列 http://web ...
 - declare命令
			
还是围绕以下几个问题进行学习; 1.declare是什么? 2.问什么要用declare? 3.怎样使用declare? 1.declare是什么? ♦declare应用的很多,向我们各种语言都会有声 ...
 - Linux系统NAT模式下设置网络网关
			
1.配置Vm网络编辑器 2.配置固定IP地址 命令:vi /etc/sysconfig/network-scripts/ifcfg-ens33 #下面内容直接复制进去,如果有重复的可以去除 TYPE= ...
 - Python-Django编程问题汇总
			
OS:Windows10 64 IDE:JetBrain Python Community Edition 2017.3.4 Python:python-3.6.4 Django:V2.0.3 问题一 ...
 - 40.oracle事务
			
一.事务特性 事务必须具备以下四个特性,简称ACID属性 原子性(Atomicity):事务是一个完整的操作.事务的各步操作是不可分割的(原子的):要么都执行,要么都不执行场景:银行转账 A-100 ...
 - JavaIO系统
			
为了方便记忆,特将IO中涉及的类进行整理如下: 1.File类 提供了目录操作,查看文件属性等. 2.java IO类层次 面向字节流的类为InputStream.OutputStream:面向字符流 ...
 - 数据挖掘 Apriori Algorithm python实现
			
该算法主要是处理关联分析的: 大多书上面都会介绍,这里就不赘述了: dataset=[[1,2,5],[2,4],[2,3],[1,2,4],[1,3],[2,3],[1,3],[1,2,3,5],[ ...
 - Struts2 漏洞系列之S2-001分析
			
0x00 前言 最近在学习java的相关漏洞,所以Struts2的漏洞自然是绕不开的.为了更好的理解漏洞原理,计划把Struts2所有的漏洞自己都做一个复现.并且自己去实现相关的POC.相关的环境 ...
 - Windows10 安装 .Net 3.5 失败的解决方案
			
最近因为使用一个公司内部的工具,需要安装.Net 3.5 SP1, 却发现无论如何都安装不上,无论是通过在线和离线安装包,还是通过Windows自带的feature安装功能,每次都是会提示错误0x80 ...