Spring:从 InitializingBean 的 afterPropertiesSet 方法发布事件不起作用
Spring: Publishing event from InitializingBean's afterPropertiesSet method does NOT work
最近我发现当我从org.springframework.beans.factory.InitializingBean.afterPropertiesSet()发布一个事件时,它无法发布该事件!
但是,如果我从@Controller 或任何其他 class class 调用它,则会调用这个完全相同的事件触发器(两个地方的事件调用机制保持相同)。
我在 InitBean ('Trigger done') 中发布事件后放置了一个打印语句,它也成功打印了。
如果您对此行为有任何想法,请告诉我。
非常感谢
//Sample code for InitializingBean:
@Component
public class InitBean implements InitializingBean {
private final ApplicationEventPublisher publisher;
public InitBean(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
@Override
public void afterPropertiesSet() throws Exception {
this.publisher.publishEvent(new TriggerEvent());
System.out.println("Trigger done");
}
}
// Sample code for trigger event:
public class TriggerEvent extends ApplicationEvent {
public TriggerEvent() {
super("source");
}
}
// Sample code for listener:
@Component
public class TriggerListener {
@EventListener(TriggerEvent.class)
public void trigger(TriggerEvent triggerEvent) {
System.out.println("Trigger event has come");
}
}
未经测试,我认为问题在于 afterPropertiesSet
在 Spring Bean 生命周期中太早了。
稍后尝试触发事件。
而是在 @PostConstruct
、初始化方法中,或者在应用程序上下文刷新事件被捕获时。
@PostConstruct
:
@Component
public class InitBean {
...
@PostConstruct
public void fire() throws Exception {
this.publisher.publishEvent(new TriggerEvent());
System.out.println("Trigger done");
}
}
初始化方法:
@Configuration
public class AppConfig {
@Bean(initMethod="fire")
public InitBean initBean (ApplicationEventPublisher publisher) {
return new InitBean (publisher);
}
}
public class InitBean {
...
@PostConstruct
public void fire() throws Exception {
this.publisher.publishEvent(new TriggerEvent());
System.out.println("Trigger done");
}
}
上下文刷新方式
@Component
public class InitBean {
...
@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
fire();
}
public void fire() throws Exception {
this.publisher.publishEvent(new TriggerEvent());
System.out.println("Trigger done");
}
}
我不知道前两种方法是否解决了疑似问题,但最后一种应该可以。
最近我发现当我从org.springframework.beans.factory.InitializingBean.afterPropertiesSet()发布一个事件时,它无法发布该事件!
但是,如果我从@Controller 或任何其他 class class 调用它,则会调用这个完全相同的事件触发器(两个地方的事件调用机制保持相同)。
我在 InitBean ('Trigger done') 中发布事件后放置了一个打印语句,它也成功打印了。
如果您对此行为有任何想法,请告诉我。
非常感谢
//Sample code for InitializingBean:
@Component
public class InitBean implements InitializingBean {
private final ApplicationEventPublisher publisher;
public InitBean(ApplicationEventPublisher publisher) {
this.publisher = publisher;
}
@Override
public void afterPropertiesSet() throws Exception {
this.publisher.publishEvent(new TriggerEvent());
System.out.println("Trigger done");
}
}
// Sample code for trigger event:
public class TriggerEvent extends ApplicationEvent {
public TriggerEvent() {
super("source");
}
}
// Sample code for listener:
@Component
public class TriggerListener {
@EventListener(TriggerEvent.class)
public void trigger(TriggerEvent triggerEvent) {
System.out.println("Trigger event has come");
}
}
未经测试,我认为问题在于 afterPropertiesSet
在 Spring Bean 生命周期中太早了。
稍后尝试触发事件。
而是在 @PostConstruct
、初始化方法中,或者在应用程序上下文刷新事件被捕获时。
@PostConstruct
:
@Component
public class InitBean {
...
@PostConstruct
public void fire() throws Exception {
this.publisher.publishEvent(new TriggerEvent());
System.out.println("Trigger done");
}
}
初始化方法:
@Configuration
public class AppConfig {
@Bean(initMethod="fire")
public InitBean initBean (ApplicationEventPublisher publisher) {
return new InitBean (publisher);
}
}
public class InitBean {
...
@PostConstruct
public void fire() throws Exception {
this.publisher.publishEvent(new TriggerEvent());
System.out.println("Trigger done");
}
}
上下文刷新方式
@Component
public class InitBean {
...
@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
fire();
}
public void fire() throws Exception {
this.publisher.publishEvent(new TriggerEvent());
System.out.println("Trigger done");
}
}
我不知道前两种方法是否解决了疑似问题,但最后一种应该可以。