- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我注册了一个 Spring ApplicationListener bean 来监听 ContextRefreshed 事件。不过,出于某种奇怪的原因,在上下文初始化完成时,我得到了对 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/
我想测试像 Example 这样的类,它处理 ContextRefreshedEvent 并在处理程序方法中连接到服务器: public class Example { @EventListe
我创建了 Spring 应用程序,并且正在独立的 Java 应用程序中加载应用程序上下文。我的代码如下 public class Application { public static voi
我正在尝试 ApplicationListener,但每次都会调用 ContextRefreshedEvent。我想知道何时调用 ContextStartedEvent。 public class C
我知道它会在 ApplicationContext 完全加载时触发一次,但是在运行时之后呢? “Refreshed”一词意味着它将在刷新时触发,但我想知道 Spring 有什么资格作为 Applica
我注册了一个 Spring ApplicationListener bean 来监听 ContextRefreshed 事件。不过,出于某种奇怪的原因,在上下文初始化完成时,我得到了对 onApp
我提出了两种情况,一种是使用 bean 工厂注册的 bean,另一种是通过自动扫描包中的注释定义(例如 @Component)创建的 bean。 bean 类使用用 @EventListener 和
在 Spring 5.x 中,以下事件有什么区别? 上下文刷新事件 上下文启动事件 上下文停止事件 上下文关闭事件 哪个事件与 servlet 上下文事件相关(根据 https://docs.orac
我想为此私有(private)方法创建 JUnit 测试: @Component public class ReportingProcessor { @EventListener pr
我试图在应用程序首次启动时进行一些数据库清理。首先,我定义了 JNDI 连接,如下所示: @Bean public TomcatEmbeddedServletContainerFactory tomc
我有一个 bean (SettingService),它用 @Transactional 注释装饰并注入(inject)到另一个 bean 中,在上下文刷新事件中调用这个 bean。 public c
我是一名优秀的程序员,十分优秀!