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,也就 ...
随机推荐
- php中的XML DOM(11)
7.创建节点 在dom操作中,增删改操作必须要找父节点 1.DOMElement DOMDocument::createElement ( string $name [, string $value ...
- Redis安装与测试
①安装,直接安装版本为4.0.1 sudo apt-get install redis-server 启动: redis-server redis-cli 测试: ②新建Student表 ③查看zha ...
- mongodb 日志清理
#!/bin/bash #Rotate the MongoDB logs to prevent a single logfile from consuming too much disk space. ...
- Qt5学习笔记(消息基础)
#include "MyWidget.h" #include <QApplication> #include <QEvent> #include <Q ...
- mxonline实战8,机构列表分页功能,以及按条件筛选功能
对应github地址:列表分页和按条件筛选 一. 列表分页 1. pip install django-pure-pagination 2. settings.py中 install ...
- leetcode-74-搜索二维矩阵
题目描述: 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 1: 输入: ...
- 对称矩阵与压缩存储算法(java实现)
package 数组和矩阵; public class DuiChenJuZhenYaSuo { int[] arr; int order; //矩阵的阶数 int n; //压缩后一位数组的个数 p ...
- python要点记录
1.字典:当存储的key数目在几万到几十万之间效率最高.
- 2016级算法期末上机-D.简单·AlvinZH's Fight with DDLs I
1117 AlvinZH's Fight with DDLs I 思路 简单题,动态规划. 本题与期末练习赛B题很相似,而且更为简单些.简化问题:在数字序列上取数,不能取相邻的数. DP数组定义,dp ...
- gerapy的初步使用(管理分布式爬虫)
一.简介与安装 Gerapy 是一款分布式爬虫管理框架,支持 Python 3,基于 Scrapy.Scrapyd.Scrapyd-Client.Scrapy-Redis.Scrapyd-API.Sc ...