Maven依赖scope属性详解–一个报错引发的问题

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>

还是同样错误,网上搜到同样情况

https://stackoverflow.com/questions/44686432/java-lang-illegalstateexception-applicationeventmulticaster-not-initialized-c?rq=1

有人说将<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/

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注