gpt4 book ai didi

java - 为什么我的 Spring ContextRefreshedevent 被调用了两次?

转载 作者:IT老高 更新时间:2023-10-28 13:50:48 24 4
gpt4 key购买 nike

我注册了一个 Spring ApplicationListener bean 来监听 Co​​ntextRefreshed 事件。不过,出于某种奇怪的原因,在上下文初始化完成时,我得到了对 onApplicationEvent(ContextRefreshedEvent) 方法的 两次 调用。这是正常行为还是表明我的配置有问题?我将 Jetty 8 用于我的 Servlet 容器。

我的相关web.xml配置如下

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring/spring-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>

谢谢!

最佳答案

即使您没有为 DispatcherServlet 指定 contextConfigLocation,它仍然会创建一个子上下文,并且第二个刷新事件是针对该上下文的。使用 event.getApplicationContext() 找出事件的上下文。

关于java - 为什么我的 Spring ContextRefreshedevent 被调用了两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6164573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com