CDI:替代BeanManager.fireEvent()的方式?

huangapple go评论99阅读模式
英文:

CDI: Replacement for BeanManager.fireEvent()?

问题

在先前的CDI版本中,javax.enterprise.inject.spi.BeanManager 上有一个名为fireEvent的方法,允许您以编程方式触发一个临时的CDI事件,而不需要先注入一个Event实例。然而,在当前的CDI版本中,似乎已经移除了该方法(尽管它仍然在Jakarta EE规范中提到)。在当前CDI版本中是否有替代方法?在CDI中执行此操作的更好方法是什么?

英文:

In a previous CDI version, there is a method on javax.enterprise.inject.spi.BeanManager called fireEvent that allows you to programmatically fire off an ad hoc CDI event, rather than requiring you to inject an Event instance first. In current CDI version, however, that method seems to have been removed (although it's still mentioned in the Jakarta EE Spec). Is there a replacement for this method in current CDI version? Is there a better way to perform this action in CDI?

答案1

得分: 3

这个方法从 CDI 2.0 版本开始被弃用。通常,任何弃用的替代方法都在javadoc中提到:

> 这个方法从 CDI 2.0 版本开始被弃用,应该使用getEvent()替代。

它在 CDI 4.0 中已经被移除

根据提到弃用的javadoc,你应该使用BeanManager#getEvent()替代。

所以,旧的形式

beanManager.fireEvent(event, qualifiers);

需要迁移到

beanManager.getEvent().select(qualifiers).fire(event);

这个新的API允许你异步触发事件,这是旧API不可能的。

beanManager.getEvent().select(qualifiers).fireAsync(event);
英文:

It was deprecated since CDI 2.0. As usual, replacement of any deprecation is mentioned in the javadoc:

> This method is deprecated from CDI 2.0 and getEvent() should be used instead.

It has been removed in CDI 4.0.

As per the javadoc where the deprecation was mentioned you should be using BeanManager#getEvent() instead.

So the old form of

beanManager.fireEvent(event, qualifiers);

needs to be migrated to

beanManager.getEvent().select(qualifiers).fire(event);

This new API allows you to fire the event asynchronously, which wasn't possible with old API.

beanManager.getEvent().select(qualifiers).fireAsync(event);

huangapple
  • 本文由 发表于 2023年8月4日 01:13:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定