What are java: comp / env scope rules?

I understand that java:comp/env is a node in the JNDI tree where you can find the properties for the current Java EE component (webapp or EJB), and I also know that each EJB has its own component environment, there is also java:global and java:app and a java:module depending on what I have some questions

  • when I use Context envContext = (Context)initContext.lookup("java:comp/env"); to get initContext, which context do I get (global, app, module, webApp or EJB Context)?
  • Are there specific rules for finding different areas?
  • Let's say I have a web application with many EJBs, does this mean that I have many initial contexts (one for webApp and one for each EJB), or all of these resources are somehow collected in the same java:comp/env context java:comp/env ?

Many thanks.

+8
java
source share
4 answers
  • when I use Context envContext = (Context) initContext.lookup ("java: comp / env"); get initContext, which context do I get (global, app, module, webApp or EJB Context)?

Quote from TomEE documentation http://tomee.apache.org/lookup-of-other-ejbs-example.html

In 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 that there is a conflict in the definition, and for EJB it is java: comp in the component (EJB itself), and not in the module as with webapps.

  1. Are there specific rules for finding different areas?

Quote from the JavaEE 6 tutorial http://docs.oracle.com/cd/E19798-01/821-1841/girgn/index.html

Java: The JNDI global namespace is a portable way to find remote Enterprise beans using JNDI queries. The java: module namespace is used to locate the local enterprise beans inside the same module. java: The application namespace is used to find local enterprise beans in packaging within a single application. That is, the enterprise bean is packaged in an EAR file containing several Java EE modules.

  1. Let's say I have a web application with many EJBs, does this mean that I have many initial contexts (one for webApp and one for each EJB), or all of these resources are somehow collected in the same java context: comp / env ?

Based on the links above, you will not have many contexts.

+1
source share

Regarding question 3 in the area of java:comp/env in the presence of EJB.

Traditionally (prior to JEE 6) java:comp/env was the module level for war modules and the EJB level for EJB in jar modules. In this model, you need to define environment entries (via resource-ref and env-entry in ejb-jar.xml , or for resource references, since JEE5 can use @Resource at the class level) for each individual 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> 

With JEE 6, you can deploy EJB as part of the war module. Regardless of whether the war module is deployed directly or is a part or ear module, it has one java:comp/env namespace java:comp/env shared between all servlets, EJBs, and any other code inside this module. You 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, there can still be ejb-jar.xml for customizing other aspects of EJB, but the env-entry for the given bean in ejb-jar.xml will eventually enter the environment value for all other beans in war .

So, I would always use war archives for everything (maybe packaged in ear ).

+1
source share
  • When I use Context envContext = (Context) initContext.lookup ("java: comp / env"); get initContext, which context do I get (global, app, module, webApp or EJB Context)?

You get the one you requested, the one you correctly described as "the current Java EE component (web application or EJB)."

  1. Are there specific rules for finding different areas?

You search within the area you specify. I do not know what else your question may mean.

  1. Let's say I have a web application with many EJBs, does this mean that I have many initial contexts (one for webApp and one for each EJB), or all of these resources are somehow collected in the same java context: comp / env ?

The question does not make sense. You create as many InitialContexts . These are objects. What they reference depends on what you are looking for in this InitialContext . If you keep looking for "the current Java EE component (a webapp, or an EJB)" , you will continue to get just that. All "these resources" are "collected" under their own names, respectively, within java:comp/env .

0
source share

As far as I know by default, the area will be global

0
source share

All Articles