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

创建监听器报错“一个或多个listeners启动失败”

问题:

今天写一个需求,即当项目启动时,取出数据库的商品类型,供全局使用,但是出现了

 创建监听器报错“一个或多个listeners启动失败”。

解决:

错误示范:

我创建了两个IOC容器

@WebListener
public class ProductTypeListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
       
        //这是又从新创建了一个IOC容器,不可以     
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext_*.xml");
        ProductTypeService productTypeService = (ProductTypeService) context.getBean("ProductTypeServiceImpl");
        List<ProductType> typeList = productTypeService.getAll();
        //放入全局应用作用域中,供新增页面,修改页面,前台的查询功能提供全部商品类别集合
        servletContextEvent.getServletContext().setAttribute("typeList",typeList);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
 <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_*.xml</param-value>
    </context-param>

正确示范:

@WebListener
public class ProductTypeListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {

        //拿到当前已经创建的IOC容器,而不是再创建一个
        WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext());
        ProductTypeService   productTypeService =(ProductTypeService) context.getBean("ProductTypeServiceImpl");
        List<ProductType> typeList = productTypeService.getAll();

        servletContextEvent.getServletContext().setAttribute("typeList",typeList);


    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

原因:

  • 重复加载 Spring 容器 ContextLoaderListener 已经通过 <context-param> 配置加载了 Spring 容器,而你又手动创建了一个新的容器,导致资源浪费。
  • Bean 不一致 :手动创建的容器与 ContextLoaderListener 加载的容器是独立的,可能导致 Bean 不一致(例如事务代理对象可能无法正常工作)。
http://www.dtcms.com/a/32384.html

相关文章:

  • Java之泛型
  • Windows安装MySQL指南
  • SAP on Microsoft Azure Architecture and Administration (Ravi Kashyap)
  • LangChain系列:精通LangChain的合并文档链
  • 【论文阅读】SAM-CP:将SAM与组合提示结合起来的多功能分割
  • LeetCode 236.二叉树的最近公共祖先
  • 抗辐照加固CAN FD芯片的商业航天与车规级应用解析
  • 常用Web性能指标
  • idea安装硅基流动中免费的deepseek(2025)
  • C++标准库——move和forward
  • Windows辉煌的发展历程
  • AMBA-CHI协议详解(十九)
  • Docker用户的困境:免费项目的减少与成本的增加
  • QT 基础知识点
  • 计算机视觉:主流数据集整理
  • python: 并发编程 (Concurrent Programming) Simple example
  • TensorFlow 是一个由 Google 开发的开源机器学习库
  • 2025tg最新免费社工库机器人
  • pipeline 使用git parameter插件实现动态选择分支构造
  • bpmn.js + Node.js_构建高效的后端工作流处理系统
  • KAJIMA CORPORATION CONTEST 2025 (AtCoder Beginner Contest 394)题解 ABCDE
  • YOLOv11-ultralytics-8.3.67部分代码阅读笔记-utils.py
  • 大湾区经济网报道:2025春运收官 全国跨区流动90亿,大湾区12亿人次
  • 支持向量机(SVM):算法讲解与原理推导
  • 线结构光三维重建
  • C语言之递归
  • 【排序算法】六大比较类排序算法——插入排序、选择排序、冒泡排序、希尔排序、快速排序、归并排序【详解】
  • 【架构】事件驱动架构(Event - Driven Architecture,EDA)
  • electron提升软件运行权限,以管理员权限运行
  • Linux 常见指令