[英]Inject EJB bean from JSF managed bean programmatically
I have EJB stateless bean. 我有EJB无状态bean。
How can I inject it to JSF managed bean by programmatic instead of @EJB annotation? 如何通过programmatic而不是@EJB注释将它注入JSF托管bean?
You can't inject it programmatically. 您无法以编程方式注入它。 You can however obtain it programmatically.
但是,您可以通过编程方式获取它。 EJBs are also available via JNDI .
EJB也可以通过JNDI获得 。 Usually, you find those JNDI names/aliases printed in server startup log.
通常,您会在服务器启动日志中找到打印的JNDI名称/别名。 At least JBoss / WildFly does that.
至少JBoss / WildFly会这样做。
There are different JNDI name aliases: 有不同的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. 其中
/APP_NAME
是WAR或EAR应用程序的名称, /MODULE_NAME
是EAR应用程序时EJB模块的名称,或者是单WAR应用程序中的WAR模块(在java:global
不存在) java:global
,否则重复/APP_NAME
), /EJB_NAME
默认为EJB类的类名。
The java:global
is accessible through the entire server. 可以通过整个服务器访问
java:global
。 The java:app
is only accessible from inside the same application (WAR or EAR). java:app
只能从同一个应用程序(WAR或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). java:module
只能从同一个模块内部访问(如果是单一WAR,则在EAR或WAR本身的情况下为EJB)。
A JSF managed bean is obviously inside a WAR. JSF托管bean显然在WAR中。 If you've a single-WAR application, then
java:module/EJB_NAME
must work. 如果您有单个WAR应用程序,则
java:module/EJB_NAME
必须正常工作。 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
. 如果您是一个EAR项目,那么EJB显然在EJB模块中,在这种情况下,
java:module
将无法工作,您需要java:app
或java:global
。
So, given an EJB like below, 所以,给定如下的EJB,
@Stateless
public class FooService {}
it's in a single-WAR project named "foo_war" via JNDI available in a JSF managed bean as follows (usually you do that in @PostConstruct
method): 它位于一个名为“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");
or in an EAR project named "foo_ear" with an EJB module named "foo_ejb" with therein the EJB class (while the JSF managed bean is in the WAR module of the EAR project): 或者在一个名为“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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.