shared items

Friday, September 5, 2008

java:comp/env

java:comp/env is a special context available to Java EE components. It provides the abstraction that each web application and ejb component has its own private naming context. The "java:comp/env" string is only used in the actual lookup() call. When defining the component dependency, it's up to the developer to pick a logical name that represents the dependency within its component naming context. There are recommendations for how to name things, e.g. prepending "jdbc" or "ejb" but those are only guidelines. The developer can choose any name he/she prefers.

Taking your example, if the code expects to do a lookup of a datasource dependency using ctx.lookup("java:comp/env/jdbc/mydb")

then the corresponding resource-ref would look like :


jdbc/mydb
javax.sql.DataSource
...


You're not the only one that has found dealing with java:comp/env a bit confusing. That's one reason the Java EE 5 platform provides an alternative to context lookups() in the form of resource injection.

You might find the following presentation about component dependencies useful :
https://glassfish.dev.java.net/javaee5/ejb/compdependencies_xmlforum_nov15.pdf

Regarding the portability of code using java:comp/env between Java EE implementations and stand-alone web servers like Tomcat, it's true that not all Java EE code can be used outside of Java EE. However, it's guaranteed to be portable to other Java EE implementations.


http://forums.java.net/jive/thread.jspa?threadID=14497

No comments: