如何通过代码注册Spring ApplicationListener实现? [英] How to register a Spring ApplicationListener implementation through code?

查看:182
本文介绍了如何通过代码注册Spring ApplicationListener实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Spring ApplicationListener 的实现。它工作正常并在上下文xml文件中声明为bean时接收事件,或者如果我使用 @Component 注释。

I've an implementation of Spring ApplicationListener. It works fine and receives events when it's declared as a bean in context xml file or if I use @Component annotation.

但是,如果我使用 ConfigurableListableBeanFactory registerSingleton()方法。

However, it doesn't receive events if I manually register it through code using the ConfigurableListableBeanFactory's registerSingleton() method.

我在下面添加了一些描述工作和非工作案例的示例代码。

I've added some sample code below that describes working and not working cases.

CustomEvent.java

CustomEvent.java

package com.test.event;

import org.springframework.context.ApplicationEvent;

public class CustomEvent extends ApplicationEvent {

    public CustomEvent(Object source) {
        super(source);
    }

    public String toString() {
        return "My Custom Event";
    }
}

CustomEventPublisher.java

CustomEventPublisher.java

package com.test.event;

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

CustomEventHandler.java

CustomEventHandler.java

package com.test.event;

import org.springframework.context.ApplicationListener;

public class CustomEventHandler 
   implements ApplicationListener<CustomEvent>{

   public void onApplicationEvent(CustomEvent event) {
      System.out.println(event.toString());
   }

}

applicationContextWithListenerBean.xml

applicationContextWithListenerBean.xml

<?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="com.test.event.CustomEventHandler"/>

   <bean id="customEventPublisher" 
      class="com.test.event.CustomEventPublisher"/>

</beans>

applicationContextWithoutListenerBean.xml

applicationContextWithoutListenerBean.xml

<?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="customEventPublisher" 
      class="com.test.event.CustomEventPublisher"/>

</beans>

MainApp.java

MainApp.java

package com.test.event;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args){

        /* The below code works fine when listener bean customEventHandler is defined in xml */

        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContextWithListenerBean.xml");
        CustomEventPublisher cvp = (CustomEventPublisher) context
                .getBean("customEventPublisher");
        cvp.publish();
        context.close();

        /* The below code doesn't work when listener bean is registered through code. Is it possible to make this work? */

        ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContextWithoutListenerBean.xml");
        context.getBeanFactory().registerSingleton("customEventHandler",
                new CustomEventHandler());
        CustomEventPublisher cvp = (CustomEventPublisher) context
                .getBean("customEventPublisher");

        cvp.publish();
        context.close();
    }
}

是否无法注册 ApplicationListener 通过代码?

Is it not possible to register an ApplicationListener through code?

推荐答案

将bean注册为单例将不会使它成为回调在ApplicationEvents上。

Registering the bean as a singleton will not enable it to be called back on ApplicationEvents.

context.getBeanFactory().registerSingleton("customEventHandler",
            new CustomEventHandler());

应更改为

context.addApplicationListener(new CustomEventHandler());

这会将ApplicationListener实现添加到 ApplicationEventMulticaster ,它将事件发布到 ApplicationListeners

This will add the ApplicationListener implementation to the ApplicationEventMulticaster which publishes events to ApplicationListeners

这篇关于如何通过代码注册Spring ApplicationListener实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆