How to create JNDI context in Spring Boot with Embedded Tomcat Container
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Spring Boot with embedded Tomcat, JNDI is not preconfigured the same way it often is in a full external application server. If you want to look up a resource such as a DataSource through JNDI, you usually need to register that resource programmatically inside the embedded Tomcat context.
The normal pattern is to customize Tomcat, enable naming, add the resource, and then point Spring at the JNDI name. This is useful when you want app-server-style resource lookup while still packaging the application as a self-contained Boot app.
Customize the Embedded Tomcat Context
A common approach is to create a TomcatServletWebServerFactory bean and register a resource on the Tomcat context:
The important part is that the resource becomes known to the embedded container before Spring tries to look it up.
Look Up the Resource from Spring
Once the resource exists, Spring can obtain it through JNDI:
This tells Spring to use the container-managed resource instead of constructing a data source directly from standard Boot properties.
Alternative Property-Based Configuration
In many cases, the simplest Spring side is just setting the JNDI property:
If the embedded Tomcat resource is already registered correctly, Boot can wire the DataSource from that property without an explicit JndiObjectFactoryBean.
Know When JNDI Is Worth the Complexity
JNDI is useful when you want deployment-time indirection or compatibility with Java EE style resource management. But in ordinary Spring Boot applications, plain spring.datasource.* configuration is often simpler and easier to maintain.
That means JNDI should be a deliberate choice, not a default habit. If you do not need container-style naming indirection, normal Spring configuration is usually the better fit.
Test the Lookup Early
A small startup check can save time when wiring this for the first time. If the application fails early with a clear JNDI lookup error, that is much easier to debug than discovering the misconfiguration only after database code executes deeper in the stack.
It also helps confirm that embedded Tomcat customization ran at all, which is often the real source of confusion.
Common Pitfalls
- Expecting embedded Tomcat to expose the same JNDI resources as an external server without extra configuration.
- Registering a resource under one name and trying to look it up under another.
- Mixing JNDI configuration with direct
spring.datasource.*settings and creating confusing behavior. - Forgetting that the embedded container must be customized before the lookup happens.
- Using JNDI when standard Boot configuration would have been simpler.
Summary
- In Spring Boot with embedded Tomcat, JNDI resources usually need to be registered programmatically.
- Customize the Tomcat context and add the resource there.
- Point Spring at the JNDI name through code or
spring.datasource.jndi-name. - Use JNDI when you actually need container-style indirection.
- Prefer plain Boot configuration when JNDI adds complexity without a clear benefit.
Related reading
- How to create liquibase changeset for integration tests in springboot?
- How to create multiple instances of KafkaReceiver in Spring Reactor Kafka
- How to Create Own HashMap in Java?
- How to create unit test with kafka embedded in the spring cloud stream
- How to customise the Jackson JSON mapper implicitly used by Spring Boot?
- How to customize DefaultHandlerExceptionResolver logic?
- How to deal with a slow SecureRandom generator?
- How to deal with java.lang.OutOfMemoryError Java heap space error?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.