← Back to list

Java Naming and Directory Interface

What is JNDI?

Avicsebooks · 2024-12-14 16:24 · 0 claps · 2.6 min read paywalled
#jndi
Open on Medium ↗
Wiki topics: 🎬 · Film & Television

Java Naming and Directory Interface

JNDI

JNDI

What is JNDI?

JNDI (Java Naming and Directory Interface) is an API in Java that provides naming and directory functionality, enabling Java applications to discover and access resources in a distributed system. It allows applications to look up objects by a name in a directory service (e.g., a JNDI registry) and retrieve those objects for use.

Why is JNDI Used?

JNDI serves several purposes in enterprise Java applications:

  1. Resource Discovery: It enables applications to discover resources (e.g., DataSources, EJBs, Mail Sessions) in a distributed environment.
  2. Decoupling: By allowing the application to look up resources at runtime, it decouples the resource configurations from the application code, making the system more flexible and easier to manage.
  3. Interoperability: It supports integration with multiple directory services like LDAP, DNS, NIS, and RMI registries.
  4. Centralized Configuration: JNDI allows administrators to define and manage resources centrally, simplifying deployment and configuration.

Practical Examples of JNDI

  1. Database Connection Pooling: JNDI is commonly used to look up a DataSource for database connections in Java EE applications.
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/MyDB");
Connection conn = ds.getConnection();

2. Mail Service Configuration: JNDI can be used to look up a JavaMail session configured on an application server.

Session mailSession = (Session) ctx.lookup("java:comp/env/mail/MyMailSession");

3. EJB Lookup: For enterprise applications, JNDI is used to find and invoke Enterprise Java Beans (EJBs).

MyEJB ejb = (MyEJB) ctx.lookup("java:global/MyApp/MyEJB");

4. JMS Connection Factories: JNDI helps in discovering connection factories and destinations for messaging systems like ActiveMQ or RabbitMQ.

QueueConnectionFactory qcf = (QueueConnectionFactory) ctx.lookup("java:comp/env/jms/MyConnectionFactory");

How Relevant is JNDI in Modern Enterprise Software Development?

While JNDI remains relevant in traditional Java EE applications, its role has diminished in modern microservices and cloud-based architectures due to the following reasons:

  1. Shift to Spring Boot and Microservices:
  • Spring Boot and microservice architectures encourage externalized configurations (e.g., using application.properties, environment variables, or tools like HashiCorp Vault).
  • The need for centralized, application-server-based JNDI resources is reduced as these frameworks manage dependencies programmatically.

2. Containerized Environments:

  • Kubernetes and Docker use different paradigms for service discovery and configuration management (e.g., ConfigMaps, Secrets, or Service Discovery).
  • Modern systems often rely on cloud-native tools, reducing reliance on JNDI.

3. Lightweight Frameworks: Lightweight frameworks like Spring Boot and Quarkus handle resource management and configuration without requiring an application server or JNDI.

Relevance:

  • JNDI is still relevant in legacy systems and Java EE environments, particularly for applications deployed on traditional application servers like WebLogic, JBoss, or WildFly.
  • In modern applications, its usage is often replaced or supplemented with lightweight, programmatic, or cloud-native solutions.

How JNDI is Used in Spring Boot

Spring Boot, being a lightweight framework, does not rely on JNDI by default but provides excellent support for integrating with JNDI when required.

Why Use JNDI in Spring Boot?

  1. Integration with Existing Java EE Infrastructure: When a Spring Boot application needs to integrate with legacy systems or application servers using JNDI-based resources.
  2. Reuse Application Server Resources: For reusing a DataSource or a JMS connection factory configured on an application server.

Example: Configuring JNDI in Spring Boot

If you have a DataSource configured in JNDI, you can access it in a Spring Boot application by configuring it in application.properties and injecting it as a bean.

  1. **application.properties Configuration:**
spring.datasource.jndi-name=java:comp/env/jdbc/MyDB

2. Java Configuration:

@Configuration public class AppConfig {
     @Bean     
      public DataSource dataSource() throws NamingException {
         JndiDataSourceLookup lookup = new JndiDataSourceLookup();
         return lookup.getDataSource("java:comp/env/jdbc/MyDB");
     } 
}

3. Using the Bean: The DataSource can now be injected into a repository or service.

@Autowired private DataSource dataSource;

Comparison: Traditional JNDI vs. Spring Boot

Comparison: Traditional JNDI vs. Spring Boot

Comparison: Traditional JNDI vs. Spring Boot


메타데이터
post_id
a06c15b0f3db
slug
java-naming-and-directory-interface-a06c15b0f3db
url
https://medium.com/@avicsebooks/java-naming-and-directory-interface-a06c15b0f3db
canonical_url
https://medium.com/@avicsebooks/java-naming-and-directory-interface-a06c15b0f3db
author_url
https://medium.com/@avicsebooks
status
ok
fetched_at
2026-06-26 21:52:29