
Step1 : maven 依赖 pom.xml 新增的监听器依赖 < dependency > < groupId > org.springframework groupId > < artifactId > spring-web artifactId > < version > 5.2.5.RELEASE version > dependency >
Step2 :注册监听器 ContextLoaderListener 若要在 ServletContext 初 始 化 时 创 建 Spring 容 器 , 就 需 要 使 用 监 听 器 接口ServletContextListener 对 ServletContext 进行监听。在 web.xml 中注册该监听器。spring3 com.it 1.0-SNAPSHOT 4.0.0 springWebwar springWeb Maven Webapp http://www.example.com UTF-8 1.8 1.8 org.springframework spring-web5.2.5.RELEASE javax.servlet javax.servlet-api3.1.0 provided javax.servlet.jsp jsp-api2.2.1-b03 provided junit junit4.11 test org.springframework spring-context5.2.5.RELEASE org.springframework spring-tx5.2.5.RELEASE org.springframework spring-jdbc5.2.5.RELEASE org.mybatis mybatis3.5.1 org.mybatis mybatis-spring1.3.1 mysql mysql-connector-java5.1.9 com.alibaba druid1.1.12 src/main/java ***.xml false src/main/resources ***.xml false maven-compiler-plugin 3.1 1.8 1.8 maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-jar-plugin 3.0.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2 maven-site-plugin 3.7.1 maven-project-info-reports-plugin 3.0.0
Spring 为该监听器接口定义了一个实现类 ContextLoaderListener ,完成了两个很重要的 工作:创建容器对象,并将容器对象放入到了 ServletContext 的空间中。 打开 ContextLoaderListener 的源码。看到一共四个方法,两个是构造方法,一个初始化 方法,一个销毁方法。 Step3 :指定 Spring 配置文件的位置RegisterServlet com.it.controller.RegisterServlet RegisterServlet /RegisterServlet contextConfigLocation classpath:applicationContext.xml org.springframework.web.context.ContextLoaderListener
Step4 :获取 Spring 容器对象 在 Servlet 中获取容器对象的常用方式有两种: ( 1 ) 直接从 ServletContext 中获取 从对监听器 ContextLoaderListener 的源码分析可知,容器对象在 ServletContext 的中存 放的 key 为 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 。所以,可以直接通过 ServletContext 的 getAttribute() 方法,按照指定的 key 将容器对象获取到。
(2) 通过 WebApplicationContextUtils 获取
工具类 WebApplicationContextUtils 有一个方法专门用于从 ServletContext 中获取 Spring 容器对象: getRequiredWebApplicationContext(ServletContext sc)package com.it.controller;
import com.it.domain.Student;
import com.it.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import java.io.IOException;
public class RegisterServlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
request.setCharacterEncoding("utf-8");//表示从客户端页面上获取过来的是utf-8的格式,去掉会产生乱码
String id = request.getParameter("id");
String name = request.getParameter("name");
String age = request.getParameter("age");
String sex = request.getParameter("sex");
String dept = request.getParameter("dept");
// 方法1.创建spring的容器对象
// String config="applicationContext.xml";
// ApplicationContext ac=new ClassPathXmlApplicationContext(config);
WebApplicationContext wac=null;
// 方法2.获取ServletContext中的容器对象,创建好的容器对象,拿来就用
// String key=WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE;
// Object attribute = getServletContext().getAttribute(key);
// if (attribute != null){
// wac= (WebApplicationContext) attribute;
// }
// 方法3.使用框架中的方法,获取容器对象
ServletContext sc=getServletContext();
wac= WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
System.out.println("容器对象的信息,======="+wac);
// 获取service
StudentService studentService = (StudentService) wac.getBean("studentService");
Student student=new Student();
student.setSno(Integer.parseInt(id));
student.setSname(name);
student.setSage(Integer.valueOf(age));
student.setSsex(sex);
student.setSdept(dept);
studentService.addStudent(student);
// 给一个页面
request.getRequestDispatcher("/result.jsp").forward(request,response);
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doPost(request,response);
}
}
以上两种方式,无论使用哪种获取容器对象,刷新
success
页面后,可看到代码中使用的
Spring
容器均为同一个对象。