How to specify the EJB bean name in the JNDI tree in JEE7

送分小仙女□ 提交于 2021-02-19 05:20:28

问题


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

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

For example:

LoginManagerBean:

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

LoginManager:

@Local
public interface LoginManager{
....
}

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

ejb:myApp/myJar/LoginManagerBean!LoginManager

In the Wildfly 10 documentation for EJB naming conventions, it says For stateless beans:

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. 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.

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:

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

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

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

回答1:


If you're using JBossEAP 7/WildFly 10.x then this is JavaEE 7, although the same answer applies to 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. Therefore these statements:

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

ejb:myApp/myJar/LoginManagerBean!LoginManager

are completely incorrect.

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

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;

    ...

}

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 {

   ...

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). Some of the names have an additional path element if you're using an EAR file.



来源:https://stackoverflow.com/questions/43282192/how-to-specify-the-ejb-bean-name-in-the-jndi-tree-in-jee7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!