英文:
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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论