我有一个 Spring 的实现ApplicationListener
。当它在上下文 xml 文件中声明为 bean 或如果我使用时,它工作正常并接收事件@Component
注解。
但是,如果我使用以下代码通过代码手动注册它,它不会接收事件ConfigurableListableBeanFactory
's registerSingleton()
方法。
我在下面添加了一些示例代码,描述了工作和不工作的情况。
自定义事件.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);
}
}
自定义事件处理程序.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>
主应用程序.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
通过代码?