java 监听 Co​​ntextRefreshedEvent

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20275952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:47:17  来源:igfitidea点击:

java listen to ContextRefreshedEvent

javaspringjavabeans

提问by kk1957

I have a classX in my spring application in which I want to be able to find out if all spring beans have been initialized. To do this, I am trying to listen ContextRefreshedEvent.

我的 spring 应用程序中有一个 classX,我希望能够在其中找出所有 spring bean 是否已初始化。为此,我正在尝试收听 ContextRefreshedEvent。

So far I have the following code but I am not sure if this is enough.

到目前为止,我有以下代码,但我不确定这是否足够。

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

public classX implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
       //do something if all apps have initialised
    }
}
  1. Is this approach correct to find out if all beans have initialsed?
  2. What else do I need to do to be able to listen to the ContextRefreshedEvent ? DO I need to register classX somewhere in xml files ?
  1. 这种方法是否正确以找出是否所有 bean 都有首字母缩写?
  2. 我还需要做什么才能收听 ContextRefreshedEvent ?我需要在 xml 文件中的某处注册 classX 吗?

采纳答案by Sotirios Delimanolis

A ContextRefreshEventoccurs

AContextRefreshEvent发生

when an ApplicationContextgets initialized or refreshed.

ApplicationContext初始化或刷新时。

so you are on the right track.

所以你在正确的轨道上。

What you need to do is declare a bean definition for classX.

您需要做的是为classX.

Either with @Componentand a component scan over the package it's in

使用@Component和组件扫描它所在的包

@Component
public class X implements ApplicationListener<ContextRefreshedEvent> {
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
       //do something if all apps have initialised
    }
}

or with a <bean>declaration

<bean>声明

<bean class="some.pack.X"></bean>

Spring will detect that the bean is of type ApplicationListenerand register it without any further configuration.

Spring 将检测到 bean 的类型ApplicationListener并注册它,无需任何进一步的配置。

Later Spring version support annotation-based event listeners. The documentationstates

后来的 Spring 版本支持基于注解的事件监听器。该文件指出

As of Spring 4.2, you can register an event listener on any public method of a managed bean by using the @EventListenerannotation.

从 Spring 4.2 开始,您可以使用@EventListener注释在托管 bean 的任何公共方法上注册事件侦听器。

Within the Xclass above, you could declare an annotated method like

X上面的类中,您可以声明一个带注释的方法,如

@EventListener
public void onEventWithArg(ContextRefreshedEvent event) {
}

or even

甚至

@EventListener(ContextRefreshedEvent.class)
public void onEventWithout() {

}

The context will detect this method and register it as a listener for the specified event type.

上下文将检测此方法并将其注册为指定事件类型的侦听器。

The documentation goes into way more detail about the full feature set: conditional processing with SpEL expression, async listeners, etc.

该文档详细介绍了完整功能集:使用 SpEL 表达式、异步侦听器等进行条件处理。



Just FYI, Java has naming conventions for types, variables, etc. For classes, the convention is to have their names start with an uppercase alphabetic character.

仅供参考,Java 有类型、变量等的命名约定。对于类,约定是让它们的名称以大写字母字符开头。

回答by Radouane ROUFID

Spring >= 4.2

弹簧 >= 4.2

You can use annotation-driven event listener as below :

您可以使用注释驱动的事件侦听器,如下所示:

@Component
public class classX  {

    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {

    }
}

the ApplicationListener you want to register is defined in the signatureof the method.

您要注册的 ApplicationListener在方法的签名中定义。

回答by Manish

I will prefer ApplicationReadyEvent. I found ContextRefreshedEvent is called before my http server is started. ApplicationReadyEvent will make sure your application is ready to take request.

我更喜欢ApplicationReadyEvent。我发现 ContextRefreshedEvent 在我的 http 服务器启动之前被调用。ApplicationReadyEvent 将确保您的应用程序已准备好接受请求。

    @EventListener(ApplicationReadyEvent.class)
    public void startApp() {
        LOGGER.info("Application is now ready!");
    }