我可以(以及如何)在 EJB 模块中使用 javax.naming.Context#lookup 查找 CDI 托管 bean?
我可以(如果可以的话,如何?)在 EJB 模块中使用 javax.naming.Context#lookup 查找 CDI 托管 bean 吗?
我正在使用 GlassFish v3。我想我可以使用 @Named
,但是 CDI 托管 bean 的 JNDI 名称是什么?我想从非托管 POJO 中查找它们,因此我无法使用 @Inject
。
Can I (and if so, how?) lookup CDI managed beans using javax.naming.Context#lookup
in EJB module?
I'm using GlassFish v3. I suppose that I can use @Named
, but what is JNDI name of CDI managed bean? I want to lookup them from unmanaged POJOs so I can't use @Inject
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以使用 CDI.current() 访问 BeanManager,这可以节省您键入几行代码。示例取自此处
CDI.current()
使用 JNDI:
现在您有了 BeanManager,您可以通过执行基于类型的查找或基于名称的查找来访问您的 CDI bean。
基于类型:
基于名称
完整示例:
更新 - 如果您使用 CDI 1.1,现在可以在一行中实现这一点:
You can also access the BeanManager by using CDI.current(), which saves you typing a good few lines of code. Example taken from here
Using CDI.current()
Using JNDI:
Now you have the BeanManager you can access your CDI beans by doing either a type-based lookup or a name-based lookup.
Type based:
Name-based
Full example:
UPDATE - This can now be achieved in one line if you are using CDI 1.1:
您可以通过 JNDI (
java:comp/BeanManager
) 查找BeanManager
,然后使用挂在BeanManager
上的 JSR-299 API 来获取对托管 bean 的上下文引用。JSR-299 托管 bean 不可用于直接 JNDI 查找。
You can lookup the
BeanManager
via JNDI (java:comp/BeanManager
) then use the JSR-299 API hung off of theBeanManager
to get a contextual reference to a managed bean.JSR-299 managed beans are not available for direct JNDI lookup.
如果您正在开发 JSF / Jakarta Faces 项目并且正在使用 OmniFaces,您可以简单地执行以下操作:
另请参阅:
If you are working on a JSF / Jakarta Faces project and you're using OmniFaces, you can simply do:
See also: