yer*_*lin 5 spring spring-boot
在某些情况下,例如在 Spring 应用程序上启用绑定@EnableBinding,ContextRefreshedEvent会开始多次被解雇。
例如,
public interface MessageBinding {
@Input("test")
KStream<Long, String> messagesIn();
}
@EnableBinding(MessageBinding.class)
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Component
public static class ComponentX {
@Autowired
ApplicationContext applicationContext;
@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
System.out.println("Fired event");
}
}
Run Code Online (Sandbox Code Playgroud)
如果删除@EnableBinding注释,ContextRefreshedEvent则只会被触发一次。
如果添加它,该事件将被触发 5 次。
您需要像这样检查您的特定 ApplicationContext:
@Autowired
ApplicationContext applicationContext;
@EventListener
public void onApplicationEvent(ContextRefreshedEvent event) {
if (event.getApplicationContext().equals(this.applicationContext)) {
System.out.println("Fired only once!");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1611 次 |
| 最近记录: |