【重温SSM框架系列】4 - Spring集成web环境(三层结构和配置监听器)

简介: 【重温SSM框架系列】4 - Spring集成web环境(三层结构和配置监听器)

三层架构环境搭建

在前面Spring核心配置文件以及数据源配置的讲解中,主要是在dao层和service层,现在我们就把web层环境也给他集成进来。

目前的项目结构

在这里插入图片描述

搭建web层

1. 引入servlet和jsp依赖

<!-- 引入Servlet和JSP依赖 -->
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>javax.servlet-api</artifactId>
   <version>3.1.0</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>

2. 创建UserServlet类

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = (UserService) application.getBean("userService");
        userService.save();
    }
}

3. 配置Tomcat并启动测试

在这里插入图片描述
启动Tomcat并访问/UserServlet,控制台打印如下,三层架构基本环境搭建成功。
在这里插入图片描述

三层架构基本项目结构

在这里插入图片描述

设置获取applicationContext.xml的监听器

现在出现了一个问题:每次从容器中获得Bean时,都需要ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml"),当配置文件多次加载时,就会创建很多个ApplicationContext对象,造成资源的浪费。

在Web项目中,我们运用监听器的特性,创建一个配置文件加载的监听器,在应用启动的时候就加载Spring的配置文件,并创建ApplicationContext 对象;当要使用时,直接从域中获取即可,达到一处加载处处使用的效果。

在web.xml中配置核心配置文件的位置

  <context-param>
    <param-name>applicationContextLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

创建一个ContextLoaderListener类

public class ContextLoaderListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContextEvent sce;
        ServletContext servletContext = servletContextEvent.getServletContext();

        // 读取web.xml中配置的Spring核心配置文件的位置
        String applicationContextLocation = servletContext.getInitParameter("applicationContextLocation");

        // 创建ApplicationContext上下文对象
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(applicationContextLocation);

        // 将ApplicationContext对象存到域中
        servletContext.setAttribute("applicationContext",applicationContext);

        System.out.println("创建ApplicationContext对象成功:" + applicationContext);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

在web.xml中配置ContextLoaderListener监听器

<listener>
  <listener-class>com.wang.listener.ContextLoaderListener</listener-class>
</listener>

启动Tomcat,查看控制台打印,发现ApplicationContext对象在Tomcat启动时被创建:
在这里插入图片描述

修改UserServlet,使用监听器获取ApplicationContext

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext application = (ApplicationContext) req.getServletContext().getAttribute("applicationContext");
        UserService userService = (UserService) application.getBean("userService");
        userService.save();
    }
}

使用Spring集成ContextLoaderListener监听器

上面通过自定义监听器的方式实现了在应用启动的时候就加载Spring的配置文件,并创建ApplicationContext 对象。但是这个过程还是比较复杂,可以使用Spring提供获取应用上下文的工具直接获取上下文对象。

引入spring-web包

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>

修改web.xml配置Spring的ContextLoaderListener监听器

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

使用Spring工具获得应用上下文对象

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext application = new ClassPathXmlApplicationContext("applicationContext.xml");
//        ApplicationContext application = (ApplicationContext) req.getServletContext().getAttribute("applicationContext");
        ApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(req.getServletContext());
        UserService userService = (UserService) application.getBean("userService");
        userService.save();
    }
}
目录
相关文章
|
30天前
|
Java 数据库连接 Maven
手把手教你如何搭建SSM框架、图书商城系统案例
这篇文章是关于如何搭建SSM框架以及实现一个图书商城系统的详细教程,包括了项目的配置文件整合、依赖管理、项目结构和运行效果展示,并提供了GitHub源码链接。
手把手教你如何搭建SSM框架、图书商城系统案例
|
1月前
|
JSON 前端开发 JavaScript
|
1月前
|
Java 数据库连接 Spring
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
文章是关于Spring、SpringMVC、Mybatis三个后端框架的超详细入门教程,包括基础知识讲解、代码案例及SSM框架整合的实战应用,旨在帮助读者全面理解并掌握这些框架的使用。
后端框架入门超详细 三部曲 Spring 、SpringMVC、Mybatis、SSM框架整合案例 【爆肝整理五万字】
|
18天前
|
SQL 安全 数据库
Web安全漏洞专项靶场—SQL注入—docker环境—sqli-labs靶场—详细通关指南
Web安全漏洞专项靶场—SQL注入—docker环境—sqli-labs靶场—详细通关指南
44 1
|
28天前
|
存储 安全 网络安全
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
|
28天前
|
前端开发 JavaScript
【Azure 环境】前端Web通过Azure AD获取Token时发生跨域问题(CORS Error)
【Azure 环境】前端Web通过Azure AD获取Token时发生跨域问题(CORS Error)
|
20天前
|
数据库 开发者 Python
web应用开发
【9月更文挑战第1天】web应用开发
34 1
|
8天前
|
数据可视化 图形学 UED
只需四步,轻松开发三维模型Web应用
为了让用户更方便地应用三维模型,阿里云DataV提供了一套完整的三维模型Web模型开发方案,包括三维模型托管、应用开发、交互开发、应用分发等完整功能。只需69.3元/年,就能体验三维模型Web应用开发功能!
32 8
只需四步,轻松开发三维模型Web应用
|
17天前
|
数据采集 Java 数据挖掘
Java IO异常处理:在Web爬虫开发中的实践
Java IO异常处理:在Web爬虫开发中的实践
|
18天前
|
前端开发 JavaScript 持续交付
Web应用开发的方法
Web应用开发的方法
13 1