What is the relationship between java:comp/env
and java:global
(regarding 3.1 spec)?
Seems like java:comp/env
contains specific to EJB references.
What means "specific" in this case?
java:comp/env is the node in the JNDI tree where you can find properties for the current Java EE component (a webapp, or an EJB). Context envContext = (Context)initContext.lookup("java:comp/env");
The Java Naming and Directory Interface™ (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the Java™ programming language. It is defined to be independent of any specific directory service implementation.
java:global
is a namespace that's global for the entire application server, which includes other EAR modules (which are considered to be different applications).
java:comp/env
is a much smaller namespace. For the web module, it corresponds to all web components (servlets etc) which are all together considered to be a single 'component' for JNDI, but for EJB beans it's a namespace for a single bean, since every bean is considered to be a separate component.
There's also a java:app
and a java:module
, whose scopes fall between global and comp.
A big difference between java:comp/env
and the others is that the former is strictly read-only at runtime and contains among others the beans that are injected into some component. So e.g. consider:
@Stateless
public class ExampleBean {
@EJB
OtherBean testBean;
}
In this case, the specific proxy that was injected into the field testBean
can also be obtained from java:comp/env
, but only when java:comp/env
is referenced from within ExampleBean (JNDI is highly contextual).
If you however wanted a different proxy to the EJB OtherBean
, or wanted a reference when no injection had been done, you can get those from any of the other scopes. Depending on from which class you're doing the JNDI call, you would be able to use smaller scopes.
For instance, if OtherBean
is defined in the same module as ExampleBean
, you could use java:module
, if it's the same application (but possibly different modules) you can use java:app
.
Finally, java:global
is always safe to use, as it doesn't depend on the context. This means you could use from within e.g. a non-managed completely separate thread. The downside to using java:global
is that you have to include the application name and the module name if an EAR is used, and otherwise at least the module name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With