我有一个Spring ApplicationListener
的实现。它工作正常并在上下文xml文件中声明为bean时或者如果我使用@Component
注释时接收事件。
但是,如果我使用ConfigurableListableBeanFactory
的{{1}}方法手动注册代码,则不会收到事件。
我在下面添加了一些描述工作而非工作案例的示例代码。
CustomEvent.java
registerSingleton()
CustomEventPublisher.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";
}
}
CustomEventHandler.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);
}
}
applicationContextWithListenerBean.xml
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());
}
}
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="customEventHandler"
class="com.test.event.CustomEventHandler"/>
<bean id="customEventPublisher"
class="com.test.event.CustomEventPublisher"/>
</beans>
MainApp.java
<?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>
是否无法通过代码注册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();
}
}
?
答案 0 :(得分:3)
将bean注册为单例将无法在ApplicationEvents上调用它。
context.addApplicationListener(new CustomEventHandler());
应改为
*.jsm
这会将ApplicationListener实现添加到ApplicationEventMulticaster,将事件发布到ApplicationListeners