java EJB 容器中可能的 JNDI 查找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15252053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Possible JNDI lookups within EJB container
提问by Steven
I'm going through the EJB 3.1 specand am trying to grasp the different possible ways a JNDI call can be made.
我正在阅读EJB 3.1 规范,并试图掌握进行 JNDI 调用的不同可能方式。
You can do it using a SessionContextand an InitialContext(or a self-created context based on the Initial- or SessionContext). Based on which you use the syntax differs, but I can't seem to find the logic behind it.
您可以使用SessionContext和InitialContext(或基于 Initial- 或 SessionContext 的自创建上下文)来实现。根据您使用的语法而有所不同,但我似乎无法找到其背后的逻辑。
So my question is: when can I use what syntax to use JNDI calls within an EJB container environment?
所以我的问题是:我什么时候可以使用什么语法在 EJB 容器环境中使用 JNDI 调用?
The rest of this question just serves as illustration of my point.
这个问题的其余部分只是为了说明我的观点。
For example, I believe this is always possible for a correctly injected sessioncontext
or created initialcontext
:
例如,我相信这对于正确注入sessioncontext
或创建始终是可能的initialcontext
:
ctx.lookup(java:global[/<app-name>]/<module-name>/<bean-name>[!<fully-qualified-interface-name>])
ctx.lookup(java:comp/env ...)
// special ones like these
ctx.lookup("java:comp/UserTransaction");
ctx.lookup("java:comp/ORB");
Sometimes (only for session context?) this shorter version is possible:
有时(仅用于会话上下文?)这个较短的版本是可能的:
ctx.lookup(<bean-name>);
What about in an embedded environment
, can only global
references be used?
在 a 中呢embedded environment
,只能使用global
引用吗?
回答by Mitja Gomboc
I usually inject EJBs inside EJB container with @EJB annotation. So the JDNI look ups are done by the server at deploy time.
我通常使用@EJB 注释将 EJB 注入到 EJB 容器中。因此 JDNI 查找由服务器在部署时完成。
For example JBOSS deployment:
例如 JBOSS 部署:
INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-2) JNDI bindings for session bean named TestBean in deployment unit subdeployment "MyEJB.jar" of deployment "MyProject.ear" are as follows:
INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor](MSC服务线程1-2)部署“MyProject.ear”的部署单元子部署“MyEJB.jar”中名为TestBean的会话bean的JNDI绑定如下:
java:global/MyProject/MyEJB/TestBean!my.project.TestBean
java:app/MyEJB/TestEJB!my.project.TestBean
java:module/TestEJB!my.project.TestBean
java:global/MyProject/MyEJB/TestEJB
java:app/MyEJB/TestBean
java:module/TestBean
Some are per EJB specification some are application server dependent.
有些是根据 EJB 规范的,有些是依赖于应用程序服务器的。
If you have to make look ups from context I think the best way is to use java:global.
如果您必须从上下文中查找,我认为最好的方法是使用java:global。
You can also find some additional info at: http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB
您还可以在以下位置找到一些其他信息:http: //glassfish.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB
回答by Mike Braun
jndi is a bit like a file system. You can refer to things using a relative path based on where you are in the tree (where you "cd"-ed to).
jndi 有点像文件系统。您可以使用基于您在树中的位置(“cd”-ed 到的位置)的相对路径来引用事物。
The injected session context is by default "positioned" on java:comp, so there you reference things that are available in java:comp, without the need to provide the "full path".
注入的会话上下文默认“定位”在 java:comp 上,因此您可以在那里引用 java:comp 中可用的内容,而无需提供“完整路径”。
Note that java:comp itself is relative to a single EJB bean, or because of historical reasons to the entire Web module.
请注意,java:comp 本身是相对于单个 EJB bean 而言的,或者由于历史原因与整个 Web 模块相关。
I'm not 100% sure what you mean with embedded environment, but if the code from which you are doing the JNDI lookup is not part of any of the predefined scopes (like java:module, java:app, etc) only java:global can be portably used.
我不是 100% 确定您对嵌入式环境的意思,但是如果您进行 JNDI 查找的代码不是任何预定义范围(如 java:module、java:app 等)的一部分,则只有 java: global 可以便携使用。