0%

spring事件监听

spring事件监听

ApplicationListener监听容器中发布的事件

实现ApplicationListener来完成事件监听

1
2
3
4
5
6
7
8
9
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);

}

可以使用publishEvent方法来发布事件

1
2
3
4
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig4.class);

context.publishEvent(new ApplicationEvent() {
});