Java 如何通过代码注册Spring ApplicationListener实现?

Java 如何通过代码注册Spring ApplicationListener实现?,java,spring,Java,Spring,我有一个SpringApplicationListener的实现。当它在上下文xml文件中声明为bean或使用@Component注释时,它工作正常并接收事件 但是,如果我使用ConfigurableListableBeanFactory的registerSingleton()方法通过代码手动注册它,它不会接收事件 我在下面添加了一些示例代码,用于描述工作和非工作案例 CustomEvent.java package com.test.event; import org.springframe

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

但是,如果我使用
ConfigurableListableBeanFactory
registerSingleton()
方法通过代码手动注册它,它不会接收事件

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

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

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

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

}

无法通过代码注册
ApplicationListener
吗?

将bean注册为单例将无法在ApplicationEvents上调用它

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

context.addApplicationListener(new CustomEventHandler());

这将向中添加ApplicationListener实现,该实现将事件发布到

我有对ApplicationContext的引用。给定这个bean和应用程序上下文,有没有办法让它通过代码接收ApplicationEvent?发布代码以显示您的工作示例和非工作示例。我已经发布了一些示例代码来描述工作和非工作案例。请看你是否能帮忙。
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();
    }
}
context.getBeanFactory().registerSingleton("customEventHandler",
            new CustomEventHandler());
context.addApplicationListener(new CustomEventHandler());