ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context
一
今天从Github
下载一个spring boot
项目,运行后报如上错误,详细如下:
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@63f509: startup date [Wed Jun 21 17:07:06 EDT 2017]; root of context hierarchy at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:404) [spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:97) ~[spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:253) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:578) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:554) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:954) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:523) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:961) [spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1033) [spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:555) [spring-context-4.3.4.RELEASE.jar:4.3.4.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE] at SpringBootExample.main(SpringBootExample.java:12) [classes/:na]
二、
1.解决
很头痛,对于spring boot
的依赖。看上面报错日志,按道理应该是包重复了,我看了下maven的依赖树,没发现重复的,上次碰到这种情况是tomcat
包依赖重复,感觉这次也是,因为按道理spring boot web
里面有tomcat
,感觉这次也是这种情况,然后我将如下依赖注释
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
还是同样错误,网上搜到同样情况
有人说将<scope>provided</scope>
注释即可,试了下果然可以,很奇怪,然后搜了下 maven 中<scope>
的用法。
2.拓展
scope
英文中的意思是范围,从使用规则上看,也符合这翻译, scope
不同的值代表相应jar
包不同的使用范围。
我们知道依赖一个jar
分为以下四种使用环节
- 编译
- 运行
- 测试
- 打包
而它的值分为以下几种:
- compile 。默认不填就是
compile
<scope>compile</scope>
和前面解决办法将scope
注释一样,注释不填默认就是如上compile
。此种情况,这个jar
参与如上所有环节
- test
这个值经常用在测试相关的jar
包上
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
- runntime
此属性表示该jar
不参与编译,但是运行、测试和打包都会参与
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
- provided
此属性表示除了最后该jar
不打包到项目中,其他前面几个环节都参与。其实就和compile
差不多,但是却被exclusion
了。通过这应该可以找出开始报错的原因了,看配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
这里将spring boot
里面自带的tomcat
剔除了。然后又配置了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
这里就很奇怪,因为spring-boot-starter
里并没有tomcat
,我们可以看看
为什么那里要exclusion
,然后因为并没有依赖spring-boot-starter-web
,所以其实这个工程是没有tomcat
的,所以下面配置tomcat
没啥毛病,但是可能provided
并不参与打包,可能这里影响了整个项目的启动。
ok,虽然最后还有点疑问,但不影响问题解决。
本文由老郭种树原创,转载请注明:https://guozh.net/mavenyilaiscopeshuxingxiangjie-yigebaocuoyinfadewenti/