Spring ApplicationListener 未接收事件

发布于 2024-11-02 04:45:21 字数 840 浏览 1 评论 0原文

我有以下 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 定义:

<bean name="myApplicationListener" class="org.mycompany.listeners.MyApplicationListener" />

我可以看到 bean 是在打印来自构造函数的消息时创建的,但从未收到上下文启动事件。我缺少什么?

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!");
  }

}

And the following bean definition:

<bean name="myApplicationListener" class="org.mycompany.listeners.MyApplicationListener" />

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

又爬满兰若 2024-11-09 04:45:21

当您在上下文上显式调用 ConfigurableApplicationContext.start() 时,会发布 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.

See also:

萧瑟寒风 2024-11-09 04:45:21

由于您没有延迟加载的bean(根据您的说法),那么您很可能出于错误的原因使用事件,并且可能应该使用类似 InitializingBean 接口:

public class MyBean implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        // ...
    }

}

来自 Spring 手册:

要与容器对 Bean 生命周期的管理进行交互,您
可以实现Spring的InitializingBean和DisposableBean
接口。容器为前者调用 afterPropertiesSet()
和 destroy() 后者允许 bean 执行某些操作
初始化和销毁​​ Bean 时的操作。你可以
也实现了与容器相同的集成,无需耦合
通过使用 init-method 将你的类连接到 Spring 接口
销毁方法对象定义元数据。

来源: Spring 框架 - 生命周期回调

Since you have no lazy loaded beans (according to you) then you are most likely using events for the wrong reason and probably should use something like InitializingBean interface instead:

public class MyBean implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {
        // ...
    }

}

From Spring manual:

To interact with the container's management of the bean lifecycle, you
can implement the Spring InitializingBean and DisposableBean
interfaces. The container calls afterPropertiesSet() for the former
and destroy() for the latter to allow the bean to perform certain
actions upon initialization and destruction of your beans. You can
also achieve the same integration with the container without coupling
your classes to Spring interfaces through the use of init-method and
destroy method object definition metadata.

Source: Spring Framework - Lifecycle callbacks

阪姬 2024-11-09 04:45:21

不确定这是否有帮助,但我隐约记得有类似的问题,这是通过预加载而不是延迟加载解决的。这是两者的快速概述

Not sure if this helps, but I vaguely remember having a similar problem, which was solved by preloading and not lazy loading. Here's a quick overview of both

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文