What does java:comp/env/ do?

Quoting https://web.archive.org/web/20140227201242/http://v1.dione.zcu.cz/java/docs/jndi-1.2/tutorial/beyond/misc/policy.html

At the root context of the namespace
is a binding with the name “comp”,
which is bound to a subtree reserved
for component-related bindings. The
name “comp” is short for component.
There are no other bindings at the
root context. However, the root
context is reserved for the future
expansion of the policy, specifically
for naming resources that are tied not
to the component itself but to other
types of entities such as users or
departments. For example, future
policies might allow you to name users
and organizations/departments by using
names such as “java:user/alice” and
“java:org/engineering”.

In the “comp” context, there are two
bindings: “env” and “UserTransaction”.
The name “env” is bound to a subtree
that is reserved for the component’s
environment-related bindings, as
defined by its deployment descriptor.
“env” is short for environment. The
J2EE recommends (but does not require)
the following structure for the “env”
namespace.

So the binding you did from spring or, for example, from a tomcat context descriptor go by default under java:comp/env/

For example, if your configuration is:

<bean id="someId" class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName" value="foo"/>
</bean>

Then you can access it directly using:

Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/foo");

or you could make an intermediate step so you don’t have to specify “java:comp/env” for every resource you retrieve:

Context ctx = new InitialContext();
Context envCtx = (Context)ctx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("foo");

Leave a Comment