Spring ApplicationListener没有收到事件 [英] Spring ApplicationListener is not receiving events
本文介绍了Spring ApplicationListener没有收到事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下ApplicationListener:
I have the following ApplicationListener:
package org.mycompany.listeners;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextStartedEvent;
public class MyApplicationListener implements ApplicationListener<ContextStartedEvent> {
public MyApplicationListener() {
super();
System.out.println("Application context listener is created!");
}
/**
* {@inheritDoc}
*/
public void onApplicationEvent(final ContextStartedEvent event) {
System.out.println("Context '" + event.getApplicationContext().getDisplayName() + "' is started!");
}
}
以及以下bean定义:
And the following bean definition:
<bean name="myApplicationListener" class="org.mycompany.listeners.MyApplicationListener" />
我可以看到在打印来自构造函数的消息时就创建了bean,但是从未收到上下文启动事件.我想念什么?
I can see that bean is created as message from the constructor is printed, but context start event is never recieved. What am I missing?
推荐答案
ContextStartedEvent
.如果您需要在初始化上下文时发布的事件,请使用ContextRefreshedEvent
.
ContextStartedEvent
is published when you explicitly invoke ConfigurableApplicationContext.start()
on the context. If you need an event that is published when context is initialized, use ContextRefreshedEvent
.
另请参见:
这篇关于Spring ApplicationListener没有收到事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文