I understand that 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), and also I know that each EJB has its own component environment ,also there is java:global
and java:app
and a java:module
depending on that I have some questions
Context envContext = (Context)initContext.lookup("java:comp/env");
to get initContext what Context exactly I get (global,app,module,webApp or EJB Context)?java:comp/env
?Many Thanks.
Quoting from TomEE documentation http://tomee.apache.org/lookup-of-other-ejbs-example.html
In a webapp, the java:comp/env namespace is shared by all servlets. This is essentially equivalent to the java:module namespace in Java EE 6. Understand there is a conflict in definition here and that for EJBs, java:comp is scoped at the component (the EJB itself) not the module as with webapps.
Quoting from JavaEE 6 Tutorial http://docs.oracle.com/cd/E19798-01/821-1841/girgn/index.html
The java:global JNDI namespace is the portable way of finding remote enterprise beans using JNDI lookups. The java:module namespace is used to look up local enterprise beans within the same module. The java:app namespace is used to look up local enterprise beans packaged within the same application. That is, the enterprise bean is packaged within an EAR file containing multiple Java EE modules.
Based on above links, you will have not have many contexts.
Regarding question 3 on the scope of java:comp/env
in presence of EJBs.
Traditionally (before JEE 6) java:comp/env
was module level for war
modules and EJB level for EJBs in jar
modules.
In that model, one has to define environment entries (via resource-ref
and env-entry
in ejb-jar.xml
, or for resource-refs since JEE5 one could use @Resource
at the class level) for every single EJB.
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>B1</ejb-name>
<ejb-class>p1.B1</ejb-class> <!-- ejb-class should be skipped if bean is already defined via annotation -->
<env-entry>
<env-entry-name>entry1</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>value1</env-entry-value
</env-entry>
</session>
</enterprise-beans>
</ejb-jar>
Since JEE 6, one can deploy EJBs as part of war
module. Whether war
module is deployed directly or is part or ear
module, it has a single java:comp/env
namespace shared between all servlets, EJBs and any other code within that module. One can define environment entries in web.xml
:
<web-app>
<env-entry>
<env-entry-name>entry1</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>value1</env-entry-value
</env-entry>
</web-app>
In this war
based model, one could still have ejb-jar.xml
to configure other aspects of EJBs, but env-entry
for a given bean in ejb-jar.xml
would end up injecting the environment value for all other beans in the war
.
So, I would always use war
archives for everything (possibly packaged in ear
).
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