gpt4 book ai didi

jsf - 以编程方式从 JSF 托管 bean 注入(inject) EJB bean

转载 作者:行者123 更新时间:2023-12-04 14:26:50 26 4
gpt4 key购买 nike

我有 EJB 无状态 bean。
如何通过编程而不是 @EJB 注释将其注入(inject) JSF 托管 bean?

最佳答案

您不能以编程方式注入(inject)它。但是,您可以通过编程方式获取它。 EJBs are also available via JNDI .通常,您会在服务器启动日志中找到这些 JNDI 名称/别名。至少 JBoss/WildFly 做到了。

有不同的 JNDI 名称别名:

java:global/APP_NAME[/MODULE_NAME]/EJB_NAME
java:app/MODULE_NAME/EJB_NAME
java:module/EJB_NAME

Where /APP_NAME is the name of the WAR or EAR application, and /MODULE_NAME is the name of the EJB module in case of an EAR application, or the WAR module in case of a single-WAR application (and this will be absent in java:global as it otherwise repeats /APP_NAME), and /EJB_NAME defaults to the class name of the EJB class.

The java:global is accessible through the entire server. The java:app is only accessible from inside the same application (WAR or EAR). The java:module is only accessible from inside the same module (EJB in case of EAR or WAR itself in case of single-WAR).

A JSF managed bean is obviously inside a WAR. If you've a single-WAR application, then java:module/EJB_NAME must work. If you've however an EAR project, then the EJB is obviously inside the EJB module, in that case the java:module won't work and you'd need java:app or java:global.

So, given an EJB like below,

@Stateless
public class FooService {}

它在一个名为“foo_war”的单 WAR 项目中,通过 JNDI 在 JSF 托管 bean 中可用,如下所示(通常您在 @PostConstruct 方法中执行此操作):
InitialContext jndi = new InitialContext();

FooService fooService = (FooService) jndi.lookup("java:module/FooService");
// Or
FooService fooService = (FooService) jndi.lookup("java:app/foo_war/FooService");
// Or
FooService fooService = (FooService) jndi.lookup("java:global/foo_war/FooService");

或者在一个名为“foo_ear”的 EAR 项目中,其中有一个名为“foo_ejb”的 EJB 模块,其中包含 EJB 类(而 JSF 托管 bean 位于 EAR 项目的 WAR 模块中):
InitialContext jndi = new InitialContext();

FooService fooService = (FooService) jndi.lookup("java:app/foo_ejb/FooService");
// Or
FooService fooService = (FooService) jndi.lookup("java:global/foo_ear/foo_ejb/FooService");

关于jsf - 以编程方式从 JSF 托管 bean 注入(inject) EJB bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29271765/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com