简体   繁体   English

如何在JEE7中的JNDI树中指定EJB bean名称

[英]How to specify the EJB bean name in the JNDI tree in JEE7

I'm not sure if this is a generic JEE6 question or if it is a Wildfly 10/JBoss7 EAP implementation specific question. 我不确定这是一个通用的JEE6问题,还是它是Wildfly 10 / JBoss7 EAP实现特定问题。

I'm trying to specify/override the default beanName used in my EJB JNDI mapping to something more meaningful to me. 我正在尝试将我的EJB JNDI映射中使用的默认beanName指定/覆盖到对我来说更有意义的东西。

For example: 例如:

LoginManagerBean: LoginManagerBean:

@Stateless
public class LoginManagerBean extends BaseManagerBean implements LoginManager {
....
}

LoginManager: LoginManager:

@Local
public interface LoginManager{
....
}

In this context, WF10 will automatically create a JNDI mapping for this EJB as: 在此上下文中,WF10将自动为此EJB创建JNDI映射,如下所示:

ejb:myApp/myJar/LoginManagerBean!LoginManager

In the Wildfly 10 documentation for EJB naming conventions , it says For stateless beans: 在针对EJB命名约定的Wildfly 10文档中,它表示对于无状态bean:

 ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface> 

.... .... .... ....

bean-name : This is the name of the bean for which you are doing the lookup. bean-name:这是您正在进行查找的bean的名称。 The bean name is typically the unqualified classname of the bean implementation class, but can be overriden through either ejb-jar.xml or via annotations. bean名称通常是bean实现类的非限定类名,但可以通过ejb-jar.xml或注释覆盖。 The bean name part cannot be an empty string in the JNDI name. bean名称部分不能是JNDI名称中的空字符串。

However, I cannot seem to find which annotation to use to specify the bean name in an annotation. 但是,我似乎无法找到用于在注释中指定bean名称的注释。 If I read the docs for @EJB it states that the beanName parameter is: 如果我读了@EJB的文档,它声明beanName参数是:

The ejb-name of the Enterprise Java Bean to which this reference is mapped 此引用映射到的Enterprise Java Bean的ejb名称

So from the docs, it does not seem that the beanName is the right parameter to use. 因此,从文档来看,beanName似乎不是正确的参数。

So how can I rename my EJB beanName in the mapping to something of my choice? 那么如何在映射中将我的EJB beanName重命名为我选择的东西? For instance, what annotation can I use to make the mapping read: 例如,我可以使用什么注释来读取映射:

ejb:myApp/myJar/MyReallyCoolName!LoginManager

If you're using JBossEAP 7/WildFly 10.x then this is JavaEE 7, although the same answer applies to Java EE 6. 如果您正在使用JBossEAP 7 / WildFly 10.x,那么这是JavaEE 7,尽管相同的答案适用于Java EE 6。

You only appear to be using Local interfaces, so none of the instructions that you linked apply because they are only for remote EJB clients . 您似乎只使用本地接口,因此您链接的所有指令都不适用,因为它们仅适用于远程EJB客户端 Therefore these statements: 因此这些陈述:

In this context, WF10 will automatically create a JNDI mapping for this EJB as: 在此上下文中,WF10将自动为此EJB创建JNDI映射,如下所示:

 ejb:myApp/myJar/LoginManagerBean!LoginManager 

are completely incorrect. 是完全错误的。

When you deploy your application all of the JNDI names are logged in the server console: 部署应用程序时,所有JNDI名称都记录在服务器控制台中:

java:global/serverapp/LoginManagerBean!com.stackoverflow.p43282192.LoginManager
java:app/serverapp/LoginManagerBean!com.stackoverflow.p43282192.LoginManager
java:module/LoginManagerBean!com.stackoverflow.p43282192.LoginManager
java:global/serverapp/LoginManagerBean
java:app/serverapp/LoginManagerBean
java:module/LoginManagerBean

Most of the time you should not care about the JNDI names because in general each EJB is unique and the server will find the right implementation: 大多数情况下,您不应该关心JNDI名称,因为通常每个EJB都是唯一的,服务器将找到正确的实现:

public class LoginClient {

    @EJB
    private LoginManager loginManager;

    ...

} }

If you want to use JNDI lookups and you want to create more work for yourself then you can specify the bean name: 如果您想使用JNDI查找并且想要为自己创建更多工作,那么您可以指定bean名称:

@Stateless(name="Foo")
public class LoginManagerBean implements LoginManager {

   ...

which yields: 产量:

java:global/serverapp/Foo!com.stackoverflow.p43282192.LoginManager
java:app/serverapp/Foo!com.stackoverflow.p43282192.LoginManager
java:module/Foo!com.stackoverflow.p43282192.LoginManager
java:global/serverapp/Foo
java:app/serverapp/Foo
java:module/Foo

and you can look these up if you must: 如果你必须,你可以看看这些:

LoginManager loginManager = (LoginManager)(new InitialContext().lookup("java:app/serverapp/Foo"));

or using injection: 或使用注射:

     @EJB(beanName="Foo")
     private LoginManager loginManager;

BTW, I'm just deploying the sample EJB jar here (serverapp.jar). 顺便说一句,我只是在这里部署示例EJB jar(serverapp.jar)。 Some of the names have an additional path element if you're using an EAR file. 如果您使用的是EAR文件,则某些名称会有一个额外的路径元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM