我有一个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());
}
}
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
<?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
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
吗? 最佳答案
将bean注册为单例将不会使其在ApplicationEvents上被调用。
context.getBeanFactory().registerSingleton("customEventHandler",
new CustomEventHandler());
应该更改为
context.addApplicationListener(new CustomEventHandler());
这会将ApplicationListener实现添加到ApplicationEventMulticaster中,该事件将事件发布到ApplicationListeners中