当前位置: 首页 > news >正文

Spring集成Web环境搭建

一、自行创建监听器来模拟springweb架构中的监听器流程

监听器的作用:当web服务器开始启动时,使用监听器监听初始化周期,并将spring配置文件放置在Servlet的最大域中。一旦服务器启动后就可以直接从域中使用gertAtribute方法去获取到spring的配置文件applicationContext.xml。

然后会设置一个工具类,通过工具类作为中介来返回配置文件信息。

自行配置逻辑应是如下:

首先是Controller层

public class UserController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        //调用applicationContext配置文件首先通过工具类WebApplicationContextUtils
        //再通过该类中的getApplicationContext从最大域中获得监听器上传的getApplicationContext配置文件内容
        //调用顺序为:WebApplicationContextUtils  ->  getApplicationContext
        // ->  (ApplicationContext) servletContext.getAttribute("applicationContext")
        ApplicationContext applicationContext =
                WebApplicationContextUtils.getApplicationContext(req.getServletContext());
        userService = (UserService) applicationContext.getBean("userService");
        userService.save();
    }

然后是web.xml配置文件

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>applicationContext.xml</param-value>
  </context-param>
<!--  监听器配置-->
  <listener>
    <listener-class>listener.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>UserController</servlet-name>
    <servlet-class>com.hxy.usercontroller.UserController</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>UserController</servlet-name>
    <url-pattern>/userController</url-pattern>
  </servlet-mapping>
</web-app>

监听器类的创建

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

        sce.getServletContext().setAttribute("applicationContext", contextConfigLocation);
        System.out.println("ContextLoaderListener contextInitialized");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}
工具类的创建
//设置一个工具,用于返回spring配置类:applicationContext
//外部只需要调用这个类的方法getApplicationContext,就能够得到返回的applicationContext
public class WebApplicationContextUtils {
    public static ApplicationContext getApplicationContext(ServletContext servletContext) {
        return (ApplicationContext) servletContext.getAttribute("applicationContext");

    }
}

二、使用spring框架中的监听器直接创建项目

只需将web.xml中的监听器配置文件改为spring-web框架自带的,同时在Controller层获取配置文件时调用spring框架自带的工具类即可,省去了创建监听器以及工具类的过程

三、总结

使用监听器的作用是简化提取配置文件的过程,方便操作。

监听器的作用在于监听web服务器的初始化过程,将配置文件进行传递,从而方便后续的获取。

通过监听器的模式,实现了解耦,如果修改了配置文件的名称或内容,将不必再在代码中重复修改,这个过程只需要在web.xml配置文件中进行相应修改即可。

http://www.dtcms.com/a/94235.html

相关文章:

  • 什么是LangChain,为什么我们选择使用LangChain,以及它的典型应用场景
  • HCIE-day15-L3VPN
  • 练习:求平方根
  • mysql数据恢复 深度扫描碎片 智能给出恢复建议并执行恢复操作
  • 【Python实用技巧】OS模块详解:文件与目录操作的瑞士军刀
  • 前端性能优化:深入解析哈希算法与TypeScript实践
  • 揭开顺序表的神秘面纱,探索数据结构的精髓
  • Vue2项目打包后,某些图片被转换为base64导致无法显示
  • 股票App开发第一步:如何免费快速的获取股票数据(如何免费获取金融数据)
  • 将网络安全和第三方风险管理与业务目标相结合
  • c++编写拉格朗日插值
  • 1.25-20GHz/500ns超快跳频!盛铂SWFA300国产捷变频频率综合器模块赋能雷达/5G/电子战高频精密控制 本振/频综模块
  • Elasticsearch:理解政府中的人工智能 - 应用、使用案例和实施
  • 深入探索C++:从基础到实践
  • Nacos Console 模块的作用是什么?是如何与 Server 端交互的?
  • 大数据专业毕业设计选题推荐:实用课题推荐
  • 基于 Redhat 9.5 的 LVS 负载均衡架构下 Nginx 服务器的部署与连通性实验
  • python.pandas.day17
  • Linux--命令行操作
  • AI for CFD入门指南(传承版)
  • centOS 7.9 65bit 修复Openssh漏洞
  • 使用QT画带有透明效果的图
  • 魔法原子,以全栈自研撬动人形机器人通用化未来
  • 【免费分享】PSO-BP特征重要性分析matlab语言
  • BFS解决最短路径问题(使用BFS解决最短路径问题的黄金法则)
  • Python生成整数序列之 range()函数
  • 《C++11:bind绑定器与function包装器》
  • Kotlin when 表达式完全指南:从基础到高级的12种实战用法
  • Redis的深入了解
  • 页面类buttons权限的简单控制