1.问题:

Springboot项目直接启动不报错,打war包放到外部容器Tomcat、东方通上,在@Weblistener注解的监听器类中报错

One or more listener fail to start

具体报错是在类构造器中,调用@Service注解的类的实例时,发生空指针异常

2.原代码:

private final XxxService xxxService;
public XxxListener(XxxService xxxService){
this.xxxService = xxxService;
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
xxxService.xxxMethod(arg0);
}

3.错误原因:

项目启动时类加载顺序问题,XxxListener构造器获取XxxService实现类的实例失败。

程序启动时会加载web.xml文件,加载顺序是context-param->listener->filter->servlet,

Spring在servlet阶段才会扫描@Controller和@Service等注解的类,

contextInitialized为servletContextListener的实现方法,在servlet之前就执行了,找不到service实例,因为此时Spring还没开始扫描和装载service。

4.解决

1.去掉构造器中赋引用的逻辑,避免启动时就先走构造器获取对象实例

2.注入方式改为如下代码,避免final使得在类加载时就初始化bean

private XxxService xxxService;

3.contextInitialized中,显式地获得 ApplicationContext 来得到由 Spring 进行管理的某些 Bean

xxxService = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()).getBean(XxxService.class);

5.新代码:

private XxxService xxxService;
@Override
public void contextInitialized(ServletContextEvent arg0) {
xxxService = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext()).getBean(XxxService.class);
xxxService.xxxMethod(arg0);
}

Springboot打包放到Tomcat中报错 One or more listener fail to start的更多相关文章

  1. vue cli3 打包到tomcat上报错问题

    首先  项目打包步骤 1.vue config.js  添加 publicPath: './', // 公共路径 assetsDir:'static', 2.将代理注释掉 proxy 3.将hash需 ...

  2. 阿里大鱼短信发送,放到项目中报错Java.lang.NoClassDefFoundError:com/aliyuncs/exceptions/ClientException,已解决

    由于项目中使用的短信服务发送的消息太慢,所以把采用了阿里大鱼的短信服务,花费了几个小时,通过审核,发现可以单独运行.但是,放到web项目中会报错(Java.lang.NoClassDefFoundEr ...

  3. springboot启动嵌入式tomcat报错找不到jar包,关键字:FileNotFoundException,derbyLocale_cs.jar,StandardJarScanner.scan

    异常: java.io.FileNotFoundException: /Users/lanhuajian/.m2/repository/org/apache/derby/derby/10.13.1.1 ...

  4. springboot注册到consul中报错:Spring MVC found on classpath, which is incompatible with Spring Cloud

    今天在做springboot整合成springCloud并注册到consul中时,发现若注册到consule中成功 则不能启动swagger,且不能提供任何API服务,要是能提供API服务则不能注册到 ...

  5. Springboot打包后,获取不到resource目录下资源文件的报错

    1.问题: java.io.FileNotFoundException ****目录下找不到模板文件 在使用Springboot启动类启动没有错,但是打包放到tomcat.东方通这些外部容器上报错,在 ...

  6. springboot去除内嵌tomcat和打包在tomcat中运行需要做的步骤

    去除内嵌tomcat和添加jsp依赖 去除内嵌tomcat 在springboot启动依赖中去除内嵌tomcat <dependency> <groupId>org.sprin ...

  7. Springboot解决war包放到Tomcat服务器上404的特殊情况

    Springboot解决war包放到Tomcat服务器上404的特殊情况 原文链接:https://www.cnblogs.com/blog5277/p/9330577.html 原文作者:博客园-- ...

  8. springboot项目中报错:listener does not currently know of SID given in connect descriptor

    springboot项目中报错:listener does not currently know of SID given in connect descriptor 出现这个问题的原因是SID写错了 ...

  9. 关于springboot 打包问题 jar包和 war包

    起因:项目开发完成   需要打包部署了  发现自己不会打包 那么开始网上学习打包? 那么怎么来打包那? 我们以前没有采用springboot 时候我们都是直接将项目打成war包形式  然后放到tomc ...

随机推荐

  1. NPM——常用命令

    npm init //创建一个package.json | npm init -y //快速全部以默认的方式生成一个package.json ,-y是-yes的缩写 以下方式可按回车默认 name 项 ...

  2. swiper基本使用

    参数名 类型 是否必填 描述 swiperContainer HTMLElement or string 必选 Swiper容器的css选择器,例如".swiper-container&qu ...

  3. Spring bean工厂配置头文件

    命名 beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  4. 写一个LRU算法的记录

    今天简单记录一下,利用Scala解答的一道LRU题目,原题为LeetCode的第146题,是一道设计LRU的题目. 题目详情 运用你所掌握的数据结构,设计和实现一个  LRU (最近最少使用) 缓存机 ...

  5. Set接口中的HashSet,LinkedHashSet,TreeSet

    TestSet package com.aff.coll; import java.util.Comparator; import java.util.HashSet; import java.uti ...

  6. C# 委托浅析

    C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针.委托(Delegate) 是存有对某个方法的引用的一种引用类型变量.引用可在运行时被改变. 委托(Delegate)特别用于实现 ...

  7. abp(net core)+easyui+efcore实现仓储管理系统——出库管理之一(四十九)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  8. Cypress系列(6)- Cypress 的重试机制

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 重试(Retry-ability) ...

  9. Java实现 LeetCode 120 三角形最小路径和

    120. 三角形最小路径和 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] ...

  10. Java实现 蓝桥杯VIP 算法提高 三角形面积

    算法提高 三角形面积 时间限制:1.0s 内存限制:256.0MB 问题描述 由三角形的三边长,求其面积. 提示:由三角形的三边a,b,c求面积可以用如下的公式: s=(a+b+c)/2 输入格式 由 ...