Springboot打包放到Tomcat中报错 One or more listener fail to start
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的更多相关文章
- vue cli3 打包到tomcat上报错问题
首先 项目打包步骤 1.vue config.js 添加 publicPath: './', // 公共路径 assetsDir:'static', 2.将代理注释掉 proxy 3.将hash需 ...
- 阿里大鱼短信发送,放到项目中报错Java.lang.NoClassDefFoundError:com/aliyuncs/exceptions/ClientException,已解决
由于项目中使用的短信服务发送的消息太慢,所以把采用了阿里大鱼的短信服务,花费了几个小时,通过审核,发现可以单独运行.但是,放到web项目中会报错(Java.lang.NoClassDefFoundEr ...
- springboot启动嵌入式tomcat报错找不到jar包,关键字:FileNotFoundException,derbyLocale_cs.jar,StandardJarScanner.scan
异常: java.io.FileNotFoundException: /Users/lanhuajian/.m2/repository/org/apache/derby/derby/10.13.1.1 ...
- springboot注册到consul中报错:Spring MVC found on classpath, which is incompatible with Spring Cloud
今天在做springboot整合成springCloud并注册到consul中时,发现若注册到consule中成功 则不能启动swagger,且不能提供任何API服务,要是能提供API服务则不能注册到 ...
- Springboot打包后,获取不到resource目录下资源文件的报错
1.问题: java.io.FileNotFoundException ****目录下找不到模板文件 在使用Springboot启动类启动没有错,但是打包放到tomcat.东方通这些外部容器上报错,在 ...
- springboot去除内嵌tomcat和打包在tomcat中运行需要做的步骤
去除内嵌tomcat和添加jsp依赖 去除内嵌tomcat 在springboot启动依赖中去除内嵌tomcat <dependency> <groupId>org.sprin ...
- Springboot解决war包放到Tomcat服务器上404的特殊情况
Springboot解决war包放到Tomcat服务器上404的特殊情况 原文链接:https://www.cnblogs.com/blog5277/p/9330577.html 原文作者:博客园-- ...
- 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写错了 ...
- 关于springboot 打包问题 jar包和 war包
起因:项目开发完成 需要打包部署了 发现自己不会打包 那么开始网上学习打包? 那么怎么来打包那? 我们以前没有采用springboot 时候我们都是直接将项目打成war包形式 然后放到tomc ...
随机推荐
- 【图机器学习】cs224w Lecture 11 & 12 - 网络传播
目录 Decision Based Model of Diffusion Large Cascades Extending the Model Probabilistic Spreading Mode ...
- java中的redis常用操作
https://blog.csdn.net/lixiaoxiong55/article/details/81592800 超详细版 常规操作 public class TestReidsComm ...
- parrot os安装drozer
dz需要支持 大部分parrot都装好了,只有Protobuf未安装 apt install Protobuf 安装dz 下面下载https://labs.f-secure.com/tools/dro ...
- word dde payload
payload: ctrl+F9 {DDEAUTO c:\\windows\\system32\\cmd.exe "/k calc.exe" } Since this techni ...
- Rocket - devices - TLDeadlock
https://mp.weixin.qq.com/s/Zv4HE7zMBzHbsWGg3pa9fg 简单介绍TLDeadlock的实现. 1. TLDeadlock TLDeadlock是抽象类Dev ...
- Rocket - debug - TLDebugModuleInner - ROM Generation
https://mp.weixin.qq.com/s/j_CgHU4PnY82NMwJzOqHYg 简单介绍Variable ROM Generation. 1. jalAbstract jalAbs ...
- CPU亲和度
CPU亲和度(CPU Affinity),就是将一个进程或者线程强制绑定在CPU的某一个core上运行. 参考:https://www.cnblogs.com/zhangxuan/p/6427533. ...
- Java实现 LeetCode 430 扁平化多级双向链表
430. 扁平化多级双向链表 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例 ...
- Java实现 LeetCode 402 移掉K位数字
402. 移掉K位数字 给定一个以字符串表示的非负整数 num,移除这个数中的 k 位数字,使得剩下的数字最小. 注意: num 的长度小于 10002 且 ≥ k. num 不会包含任何前导零. 示 ...
- Java实现第十届蓝桥杯外卖店优先级
试题 G: 外卖店优先级 时间限制: 1.0s 内存限制: 512.0MB 本题总分:20 分 [问题描述] "饱了么"外卖系统中维护着 N 家外卖店,编号 1 ∼ N.每家外卖店 ...