Quick revision
The short answer
JNDI (Java Naming and Directory Interface) is a Java API for looking up objects by name. An application server binds resources such as DataSources, JMS connection factories and mail sessions under names like java:comp/env/jdbc/orders, and the application gets them with new InitialContext().lookup(name) or, better, by injection with @Resource(lookup = "java:comp/env/jdbc/orders"). Spring Boot usually avoids JNDI by building the DataSource itself from spring.datasource properties, and offers spring.datasource.jndi-name for the cases where it is deployed on a server that owns the pool.
What to remember
In this answer, we saw that JNDI is a naming API that application servers use to bind resources such as DataSources, JMS factories and mail sessions under names like java:comp/env/jdbc/orders. Code can look them up with InitialContext, but injection with @Resource is easier to test. The java:comp, java:module, java:app and java:global namespaces set how widely a name is visible. Spring Boot normally builds its DataSource from spring.datasource properties and uses spring.datasource.jndi-name only when a server owns the pool.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
JNDI (Java Naming and Directory Interface) is a Java API for looking up objects by name. An application server binds resources such as DataSources, JMS connection factories and mail sessions under names like java:comp/env/jdbc/orders, and the application gets them with new InitialContext().lookup(name) or, better, by injection with @Resource(lookup = "java:comp/env/jdbc/orders"). Spring Boot usually avoids JNDI by building the DataSource itself from spring.datasource properties, and offers spring.datasource.jndi-name for the cases where it is deployed on a server that owns the pool.
Background to this question
JNDI is one of those technologies that many developers meet only when they work on an older system or deploy to an application server, and then it seems mysterious. It is worth understanding because it explains how application servers separate configuration from code, why Spring Boot works differently, and why JNDI appeared in serious security incidents. In this answer, we will recap what JNDI is, see how a resource gets bound and looked up, learn what the java:comp, java:module, java:app and java:global names mean, compare lookup with injection, and see how Spring Boot handles the same need.
1A Quick Recap of JNDI
JNDI is a naming API, part of Java since version 1.3. A naming service maps names to objects, a bit like a phone book maps names to numbers. The same API can talk to different naming systems, such as an application server's internal registry, an LDAP directory or an RMI registry, through pluggable providers. For enterprise applications, the important use is the server's own registry.
The idea is a separation of duties. An administrator configures a connection pool on the server, with the database URL, the user, the password and the pool size, and binds it under a name. The application only knows the name. So the same WAR file can be deployed to test and production, and each server gives it a different database behind the same name.
2Looking Up a DataSource
The classic way is a programmatic lookup through InitialContext, the entry point to the naming service. The newer and better way is to let the container inject the resource with @Resource, so our class never calls the naming API itself.
Notice the name. java:comp/env is the component's private environment, and jdbc/orders is a logical name that the deployment maps to the real pool, either through a resource-ref in web.xml or a server-specific binding. The reason for this extra indirection is that two applications can both call their database jdbc/orders while the server points each at a different pool.
3What java:comp, java:module, java:app and java:global Mean
The java:comp namespace dates from early J2EE, and Java EE 6 added java:module, java:app and java:global, so that together they define how widely a name is visible. A name under java:comp is visible only to one component, java:module to all components in one module such as one WAR, java:app to all modules in one application (an EAR), and java:global to every application on the server. This lets us share a resource exactly as widely as needed.
| Namespace | Visible to | Example |
|---|---|---|
| java:comp | One component (in a WAR, the whole web module) | java:comp/env/jdbc/orders |
| java:module | All components in one module | java:module/OrderService |
| java:app | All modules in one application (EAR) | java:app/jdbc/shared |
| java:global | Every application on the server | java:global/jdbc/reporting |
4Why Injection Beats Hard-coded Lookups
A hard-coded lookup ties the class to a running naming service. In a unit test there is no InitialContext, so new InitialContext().lookup throws a NamingException, and we have to build a fake naming context just to test business logic. The lookup also hides the dependency inside a method, so readers cannot see from the constructor what the class needs.
With injection, the class simply has a DataSource field or constructor parameter. In production the container fills it from JNDI, and in a test we pass an embedded or containerised database. So we keep JNDI, which still works well for server-managed resources, but we keep it out of our code.
5How Spring Boot Avoids JNDI
Spring Boot applications usually own their resources. Auto-configuration sees spring.datasource properties and creates a HikariCP connection pool inside the application, so there is no server and no JNDI name at all. Different environments get different databases through environment variables or configuration files.
The second form exists for deployments on Tomcat, WildFly or WebLogic where operations want to manage the pool in the server. When spring.datasource.jndi-name is set, Spring Boot looks the DataSource up instead of creating one. So JNDI becomes a deployment choice rather than something baked into the code.
6Follow-up questions
What is the difference between @Resource(name = ...) and @Resource(lookup = ...)?
name declares a logical name in the component's environment (java:comp/env/...) that the deployment maps to a real resource. lookup points directly at a global or portable JNDI name, such as java:global/jdbc/reporting, without that mapping step.
Is JNDI used for anything besides resources?
Yes. It can look up EJBs by their portable names, environment entries such as configuration strings, and objects in LDAP directories. That generality, especially LDAP and RMI providers, is why JNDI was misused in the Log4Shell vulnerability.
Does embedded Tomcat in Spring Boot support JNDI?
It is disabled by default. It can be enabled by customising the embedded Tomcat, but this is rarely worth it; injecting beans configured from properties is simpler.
7Conclusion
In this answer, we saw that JNDI is a naming API that application servers use to bind resources such as DataSources, JMS factories and mail sessions under names like java:comp/env/jdbc/orders. Code can look them up with InitialContext, but injection with @Resource is easier to test. The java:comp, java:module, java:app and java:global namespaces set how widely a name is visible. Spring Boot normally builds its DataSource from spring.datasource properties and uses spring.datasource.jndi-name only when a server owns the pool.
Open with
JNDI is the Java Naming and Directory Interface, an API for looking up objects by name. Application servers use it as a registry: an administrator configures a DataSource, a JMS connection factory or a mail session on the server and binds it under a name, for example java:comp/env/jdbc/orders, and the application only knows that name.
Then, if they want more
That separates configuration from code, so the same WAR runs against different databases in test and production.
The application can look the resource up with new InitialContext().lookup and cast it to DataSource, but the better approach is injection, @Resource with the lookup name, because then the class does not depend on a running naming service and is easy to test with a different DataSource.
The prefixes define scope: java:comp for one component, java:module for one module, java:app for one application in an EAR, and java:global for the whole server. Spring Boot usually does not need any of this. Its auto-configuration builds a HikariCP pool from spring.datasource.url, username and password, and each environment just supplies different properties.
If we deploy a Spring Boot WAR to a server whose administrators manage the pool, we can set spring.datasource.jndi-name and Spring will look it up instead of creating one.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Jakarta EE specifications
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is JNDI, how does a Jakarta EE application look up a DataSource with it, and how...": https://scrivio-interview-library.netlify.app/java/jakarta-ee-platform/4513ed5aae/