CDI。如何检查bean是否实例化? [英] CDI. How to check whether bean instantiated or not?

查看:67
本文介绍了CDI。如何检查bean是否实例化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CDI @Named bean的名字。例如, firedEmployeeBean。

I've got a name of CDI @Named bean. For example, 'firedEmployeeBean'.

其他CDI bean中是否有任何方法可以检查 firedEmployeeBean是否已实例化?

Is there any way in other CDI bean check whether 'firedEmployeeBean' already instantiated or not?

推荐答案

如前所述,如果您使用 @Inject 进行检查,则可以使用。相反,您想要的是拥有一个属性来告诉您您想要什么:

As already stated if you use @Inject once you check it will be. What you instead want is to have a property to tell you what you want:

boolean initiated;

如果这个简单的解决方案不能解决问题,我建议使用Deltaspike:

if this simple solution does not cut it I would recommend to use Deltaspike:

MyBean myBean = BeanProvider.getContextualReference(MyBean.class, true);

请注意第二个参数true-来自文档:如果将true作为第二个参数传递查找给定接口的实现,并且不需要实现或不需要具有给定限定符的实例(有关更多详细信息,请参见限定符示例)。 http://incubator.apache.org/deltaspike/core.html

Note the second argument, true - from docs: Pass true as second argument, if you look for an implementation of the given interface and an implementation isn't required or it isn't required that there is an instance with the given qualifier (see the qualifier example for further details). http://incubator.apache.org/deltaspike/core.html

最后您可以使用事件。在CDI中使用事件真的很简单。您需要做的是在创建bean时触发该事件,并让另一个bean观察该事件。
http://docs.jboss.org /weld/reference/latest/zh-CN/html/events.html

Lastly you can use events. Events are really simple to use in CDI. What you need to do is fire the event when the bean is created and have the other bean observe that event. http://docs.jboss.org/weld/reference/latest/en-US/html/events.html

这篇关于CDI。如何检查bean是否实例化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆