Spring与web环境的集成
基础web环境
其他与前期学过的做法都一样
需要在pom.xml配置文件下 添加坐标
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
在web.xml文件中的内容
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.ccj.web.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
</web-app>
UserServlet中的内容
@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
上述操作完成后,需要将项目配置进Tomcat中进行测试,是否配置成功。
配完成之后,启动Tomcat,在弹出的浏览器输出框内,显示的url后添加useServlet
此时网页跳转到的为空白页面,并不是找不到。此时查看编译器的控制台。当控制台上打印出 save running… 字样时 说明配置成功。
ContextLoaderListener监听器的分析
应用上下文对象ClassPathXmlApplicationContext(“applicationContext.xml”)(spring配置文件)方式获取的,但是每次从容器获得Bean时都要编写ClassPathXmlApplicationContext(“applicationContext.xml”),这样的弊端时配置问价加载多次,应用上下文创建多次。
在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得ApplicationContext对象.
步骤
- 在com.ccj下创建listener包
- 创建类ContextLoaderListener
public class ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
//将spring的应用上下文对象存储到最大的域(servletContext域)中
ServletContext servletContext = sce.getServletContext();
servletContext.setAttribute("app",app);
System.out.println("spring容器创建完毕");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContextListener.super.contextDestroyed(sce);
}
}
- web.xml配置文件中添加监听器配置
<!-- 配置监听器-->
<listener>
<listener-class>com.ccj.listener.ContextLoaderListener</listener-class>
</listener>
- 将UserServlet修改为如下,并进行测试
@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ServletContext servletContext = this.getServletContext();
ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
测试结果
再对上述代码进行优化
ApplicationContext app = new ClassPathXmlApplicationContext(“applicationContext.xml”);
在监听器ContextLoaderListener中上面这一句显然时耦合死的,要是以后需要改变applicationContext的名称就不能很好的修改。
解决办法:因为实在web环境中,所以在web.xml文件设置一个全局参数
设置方法
第一步.web.xml
<!-- 全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
contextConfigLocation是配置上去的名称,下面applicationContext.xml是配置的xml文件。实现之后就可以在配置文件中更改所需要更改的xml配置文件名称,不需要在类内部再去修改。
第二部.更改监听器ContextLoaderListener设置
此处String contextConfigLocation = servletContext.getInitParameter(“contextConfigLocation”);就是调用了上面web.xml中的配置
public class ContextLoaderListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
//读取web.xml中的全局参数
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
//将spring的应用上下文对象存储到最大的域(servletContext域)中
servletContext.setAttribute("app",app);
System.out.println("spring容器创建完毕");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContextListener.super.contextDestroyed(sce);
}
}
在ContextLoaderListener中获取应用上下文之后及servletContext.setAttribute(“app”,app);是放置在最大的域之中了,这个名字“app”也是耦合死的。要想使用这个最大的域的客户端都要记住这个名字,每一次从UserServlet中获取一个app,万一之后改名字不叫app了,也得要用户记住的话。
所以在开发之中,要将具体的名称隐藏掉。那如何隐藏呢?
解决方法就是,你只要向我要,我就直接把应用上下文ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);的app给你.那就需要去设置一个工具。这个工具也需要一个参数servletContext,及上下文对象。
步骤
第一步.在listener包中创建一个WebApplicationContextUtils工具类,并设置一个工具方法
public class WebApplicationContextUtils {
public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
return (ApplicationContext) servletContext.getAttribute("app");
}
}
第二步.修改UserServlet中的ApplicationContext app = (ApplicationContext) servletContext.getAttribute(“app”);
@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ServletContext servletContext = this.getServletContext();
//ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
Spring提供获取应用上下文的工具
上面的分析不需要手动实现,Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部记载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。
所以说我们需要做两件事:
- 在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
- 使用WebApplicationContextUtils获取应用上下文对象ApplicationContext
pom.xml添加
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
web.xml配置
<!-- 全局初始化参数-->
<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>
UserServlet更改
import com.ccj.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ServletContext servletContext = this.getServletContext();
//ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
// ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);
userService.save();
}
}