如何在JEE7的JNDI树中指定EJB bean名称 [英] How to specify the EJB bean name in the JNDI tree in JEE7

查看:74
本文介绍了如何在JEE7的JNDI树中指定EJB bean名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定这是一个通用的JEE6问题还是一个Wildfly 10/JBoss7 EAP实现特定问题.

I'm not sure if this is a generic JEE6 question or if it is a Wildfly 10/JBoss7 EAP implementation specific question.

我试图指定/覆盖在我的EJB JNDI映射中使用的默认beanName到对我更有意义的东西.

I'm trying to specify/override the default beanName used in my EJB JNDI mapping to something more meaningful to me.

例如:

LoginManagerBean:

LoginManagerBean:

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

LoginManager:

LoginManager:

@Local
public interface LoginManager{
....
}

在这种情况下,WF10将自动为该EJB创建JNDI映射,如下:

In this context, WF10 will automatically create a JNDI mapping for this EJB as:

ejb:myApp/myJar/LoginManagerBean!LoginManager

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

........

bean-name:这是您要为其执行操作的bean的名称.抬头.Bean名称通常是bean实现类,但可以通过任一方法覆盖ejb-jar.xml或通过注释.Bean名称部分不能为空JNDI名称中的字符串.

bean-name : This is the name of the bean for which you are doing the lookup. 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. The bean name part cannot be an empty string in the JNDI name.

但是,我似乎无法找到用于在注释中指定bean名称的注释.如果我阅读了 @EJB 的文档,则会指出 beanName 参数为:

However, I cannot seem to find which annotation to use to specify the bean name in an annotation. If I read the docs for @EJB it states that the beanName parameter is:

此引用映射到的Enterprise Java Bean的ejb名称

The ejb-name of the Enterprise Java Bean to which this reference is mapped

因此,从文档来看,beanName似乎并不是要使用的正确参数.

So from the docs, it does not seem that the beanName is the right parameter to use.

那么如何在映射中将我的EJB beanName重命名为我选择的东西?例如,我可以使用什么注释使映射读取:

So how can I rename my EJB beanName in the mapping to something of my choice? For instance, what annotation can I use to make the mapping read:

ejb:myApp/myJar/MyReallyCoolName!LoginManager

推荐答案

如果您使用的是JBossEAP 7/WildFly 10.x,那么这就是JavaEE 7,尽管相同的答案也适用于Java EE 6.

If you're using JBossEAP 7/WildFly 10.x then this is JavaEE 7, although the same answer applies to Java EE 6.

您似乎只在使用本地接口,因此链接的说明均不适用,因为它们仅用于远程EJB客户端.因此,这些语句:

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. Therefore these statements:

在这种情况下,WF10将自动为该EJB创建JNDI映射,如下:

In this context, WF10 will automatically create a JNDI mapping for this EJB as:

ejb:myApp/myJar/LoginManagerBean!LoginManager

完全不正确.

部署应用程序时,所有JNDI名称都记录在服务器控制台中:

When you deploy your application all of the JNDI names are logged in the server console:

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

大多数时候,您不必关心JNDI名称,因为通常每个EJB都是唯一的,并且服务器会找到正确的实现:

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:

public class LoginClient {

    @EJB
    private LoginManager loginManager;

    ...

}

如果要使用JNDI查找,并且想自己创建更多工作,则可以指定Bean名称:

If you want to use JNDI lookups and you want to create more work for yourself then you can specify the bean name:

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

   ...

产生:

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"));

或使用注射:

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

顺便说一句,我只是在这里部署示例EJB jar(serverapp.jar).如果您使用的是EAR文件,则其中一些名称还会包含一个额外的path元素.

BTW, I'm just deploying the sample EJB jar here (serverapp.jar). Some of the names have an additional path element if you're using an EAR file.

这篇关于如何在JEE7的JNDI树中指定EJB bean名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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