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注入容器)的更多相关文章

  1. Spring学习笔记四 整合SSH

    三大框架架构(整合原理) 步骤1:导包 Hibernate包 1.Hibernate包,hibernate/lib/required 2.hibernate/lib/jpa | java persis ...

  2. Spring学习笔记(1)——初识Spring

    一.Spring是什么       通常说的Spring其实指的是Spring Framework,它是Spring下的一个子项目,Spring围绕Spring Framework这个核心项目开发了大 ...

  3. Spring学习笔记(二) 初探Spring

    版权声明 笔记出自<Spring 开发指南>一书. Spring 初探 前面我们简单介绍了 Spring 的基本组件和功能,现在我们来看一个简单示例: Person接口Person接口定义 ...

  4. Spring学习笔记之整合struts

    1.现有项目是通过 <action    path="/aaaaAction"                type="org.springframework.w ...

  5. Spring学习笔记之整合hibernate

    1.web.xml里边要配置好对应的springxml的路径 <context-param> <param-name>contextConfigLocation</par ...

  6. Spring 学习笔记之整合Hibernate

    Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心SQ ...

  7. 【Spring学习笔记-1】Myeclipse下Spring环境搭建

    *.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...

  8. Spring 学习笔记(一):Spring 入门

    1 Spring简介 Spring是一个轻量级Java开发框架,最早由Rod Johnson创建,目的是为了解决企业级应用开发的业务逻辑层和其他各层的耦合问题,是一个分层的Java SE/EE ful ...

  9. Spring学习笔记之一----基于XML的Spring IOC配置

    1. 在spring配置文件中,如果对一个property进行直接赋值,可使用<value>元素,spring负责将值转化为property指定的类型:也可以直接在property元素上使 ...

  10. Java框架spring 学习笔记(九):Spring的bean管理(@Required、@Component、@Autowired、@Resource注解)

    注解:代码里面特殊的标记,使用注解可以完成相关功能 注解写法:@注解名称(属性名.属性值) @Required 用在set方法上,一旦用了这个注解,那么容器在初始化bean的时候必须要进行set,也就 ...

随机推荐

  1. Java中的内部类(二)成员内部类

    Java中的成员内部类(实例内部类):相当于类中的一个成员变量,下面通过一个例子来观察成员内部类的特点 public class Outer { //定义一个实例变量和一个静态变量 private i ...

  2. Remoteland HDU - 4196

    题意: 给出一个n,在[1, n] 中挑选几个不同的数相乘,求能的到的最大完全平方数 解析: 最大的肯定是n!, 然后n!不一定是完全平方数 (我们知道一个完全平方数,质因子分解后,所有质因子的质数均 ...

  3. linux系统无法启动,提示give root password for maintenance错误

    电脑的虚拟机安装的是centos6.2操作系统,今天打开虚拟机时候,提示 give root password for maintenance (or type control-D to contin ...

  4. HTML5语义化标签总结

    1.语义化标签总结 基础布局标签 <header></header> <nav></nav> <main></main> < ...

  5. 干货 | Elasticsearch Nested类型深入详解(转)

    https://blog.csdn.net/laoyang360/article/details/82950393 0.概要在Elasticsearch实战场景中,我们或多或少会遇到嵌套文档的组合形式 ...

  6. 爬虫5:beautifulsoup

      灵活方便的网页解析库,处理高效,支持多种解析器,利用它不用编写正则表达式即可方便的实现网页信息的提取     一. BeautifulSoup的几种解析库   解析器 使用方法 优势 劣势 Pyt ...

  7. eclipse首次使用的基本设置

    最近,一些刚开始学习Java的朋友使用eclipse遇到了一些编码导致的问题向我询问,那就总结一下首次安装eclipse后我们大体应该设置哪些基本东西吧~大神们呐就不用看啦. 一.修改工作空间默认编码 ...

  8. 函数直接写在html页面的<script>里可以调用,但是单独放在js文件里不能调用

    1.函数直接写在页面相当于是你本页调用,所以理所应当可以调用 2.js单独文件不能调用是因为你没有引用js文件,如果引用了的话,也是可以调用的. 引用方式,你可以直接拖拽(我一般都是拖拽,因为路径准确 ...

  9. vsftpd文件虚拟用户搭建

    关于vsftpd的原理这里就不多说了,下面红色部分有单独标出,突出显示,意思是这里的东西有额外的配置,全文的配置一定要跟着第二步的配置来,不要过程中随便改变参数,除非你看得懂,好了直接上配置过程 1. ...

  10. Mac 10.12常用软件清单

    链接: https://pan.baidu.com/s/1slds1OD 密码: 7m5t 配套教程:http://www.cnblogs.com/EasonJim/tag/mac/ 如果失效了,联系 ...