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. SQL——MySQL数据类型

    Text类型: Number类型: Date类型:

  2. JUC整理笔记三之测试工具jcstress

    并发测试工具Jcstress使用教程 Jcstress 全称 Java Concurrency Stress,是一种并发压力测试工具,可以帮助研究JVM.java类库和硬件中并发的正确性. Wiki地 ...

  3. BUUCTF Crypto

    BUUCTF 几道crypto WP [AFCTF2018]Morse 简单的莫尔斯密码,最直观的莫尔斯密码是直接采用空格分割的点和划线,这题稍微绕了一下使用的是斜杠来划分 所以首先将斜杠全部替换为空 ...

  4. Hello, CTF

    0x01 拿到题目后查壳,发现什么也没有,32位vc++ 0x02 放到IDA里,F5反编译,得到下图 很容易我们就看到了比较的函数,以及出现wrong和success的字符串,所以接下来就是仔细分析 ...

  5. Unable to start services. See log file /tmp/vmware-root/vmware-6853.log for details.

    debian安装vmware错误 https://github.com/AdministratorGithub/vmshell vm15.1.0解决linux安装出现Unable to start s ...

  6. 16 . PythonWeb框架之Django

    Web框架简介 Web开发是Python语言应用领域的重要部分,也是目前最主要的Web开发语言之一,在其二十多年的历史中出现了数十种Web框架,比如Django.Tornado.Flask.Twist ...

  7. Redis详解(十一)------ 过期删除策略和内存淘汰策略

    在介绍这篇文章之前,我们先来看如下几个问题: ①.如何设置Redis键的过期时间? ②.设置完一个键的过期时间后,到了这个时间,这个键还能获取到么?假如获取不到那这个键还占据着内存吗? ③.如何设置R ...

  8. Rocket - regmapper - RegField

    https://mp.weixin.qq.com/s/7WKB1QxcVzqm2Q7bWcKHzA 简单介绍RegField的实现. 1. 简单介绍 定义寄存器域相关的参数类型. 2. RegFiel ...

  9. Rocket - util - ECC

    https://mp.weixin.qq.com/s/yato1PrnHe517J8twgZFOg   介绍ECC(Error Correcting Code/Error Checking and C ...

  10. Username for 'https://github.com': remote: Invalid username or password. fatal: Authentication failed for 'https://github.com/GLSmile/pythontest.git/' 问题

    使用$ git push -u origin master 进行同步时,提示输入用户名和密码,但是我输入正确的信息后,仍然 会报Username for 'https://github.com': r ...