java - 事件仅由一个 bean 接收? (尽管有好几个)

标签 java spring

我对此感到困惑。我创建了三个应该监听事件的 bean 实例,但只有其中一个捕获了事件。为什么?请参阅下面的代码和输出。附言。 Spring 新手。

事件处理类

package customevents.di;

import org.springframework.context.ApplicationListener;

public class CustomEventHandler implements ApplicationListener<CustomEvent> {

    private static int ID = 0;

    public CustomEventHandler() {
        ID++;
        System.out.println("Constructor called CustomEventHandler " + ID);

    }

    public void onApplicationEvent(CustomEvent event) {
        System.out.println(event.toString() + " " + ID); // Prints My Custom event and ID
    }

}

发布者

package customevents.di;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

public class CustomEventPublisher implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher publisher;

    public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {

        this.publisher = publisher;
    }

    public void publish() {
        CustomEvent ce = new CustomEvent(this);
        publisher.publishEvent(ce);
    }
}

用法

  public static void main(String[] args) {
      ConfigurableApplicationContext context = 
      new ClassPathXmlApplicationContext("Beans.xml");

      CustomEventPublisher cvp = 
      (CustomEventPublisher) context.getBean("customEventPublisher");

      // Scope of these beans is prototype
      CustomEventHandler ce =  (CustomEventHandler) context.getBean("customEventHandler");
      CustomEventHandler ce1 =  (CustomEventHandler) context.getBean("customEventHandler");
      CustomEventHandler ce2 =  (CustomEventHandler) context.getBean("customEventHandler");

      cvp.publish();

   }

输出

Constructor called CustomEventHandler 1
Constructor called CustomEventHandler 2
Constructor called CustomEventHandler 3
Constructor called CustomEventHandler 4
My Custom Event 4

为什么只有一个bean收到事件?

附言。按要求添加 beans 文件。

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="customEventHandler" class="customevents.di.CustomEventHandler" scope="prototype">      
   </bean>

   <bean id="customEventPublisher" class="customevents.di.CustomEventPublisher">      
   </bean>

</beans>

最佳答案

更新1:

Staphanee Nicoll已在 spring blog post 中描述如下:

The actual processing of the event will respect the semantics you set on your bean. So if you set prototype we are going to create a new instance of the bean for you prior to call the method on it. And we're going to do that for every event. If you expect all your existing prototype instances to be called for a particular event, that's not going to happen (we never did multicast an event and that infrastructure is not meant to do that).


您错过了添加 cvp.publish() 来调用每个自定义事件处理程序。

另一个问题 static 部分已由 costi-ciudatu 描述

希望它能奏效。

要了解更多信息,您可以阅读本教程:Custom events in spring

Github 资源链接:

Spring app event demo

关于java - 事件仅由一个 bean 接收? (尽管有好几个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37799265/

相关文章:

java - 从闰年计算年数

java - 同时测试 Clojure 和 Java

java - 从 JSON Volley 请求中获取值的正确方法是什么?

java - 如何通过 Spring 中的并发调用来限制方法?

java - Spring Maven依赖问题

spring - 如何防止两个用户编辑数据库中的一行

java - 需要解释继承关系

java - 强制开发人员显式定义配置数据的键

java - 尝试使用hibernate从MySQL获取数据,并显示在thymeleaf列表中(循环)

java - 如何将 Pdf View 从 Spring 返回到 angularjs