Quick look of JNDI (Java Naming and Directory Interface)

Simple to say, programmer can use the same JNDI interface to query the following

lookup resources provided by application server, such as data source
search LDAP entries
lookup DNS records

Brief introduction is here.

The code

Resources …


This content originally appeared on DEV Community and was authored by Salad Lam

Simple to say, programmer can use the same JNDI interface to query the following

  • lookup resources provided by application server, such as data source
  • search LDAP entries
  • lookup DNS records

Brief introduction is here.

The code

Resources of the application server are placed under "java:comp/env" prefix. Assume that a data source is on

java:/comp/env/jdbc/db1

To get that data source

javax.naming.Context initialContext = new javax.naming.InitialContext();
javax.naming.Context subContext = (javax.naming.Context) initialContext.lookup("java:comp/env");
DataSource dataSource = (DataSource) subContext.lookup("jdbc/db1");

Or you may get the instance directly by providing the full path

javax.naming.Context initialContext = new javax.naming.InitialContext();
DataSource dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/db1");

Spring boot integration

Just add a line into application.properties

spring.datasource.jndi-name=java:/comp/env/jdbc/db1

The related bean is created by org.springframework.boot.autoconfigure.jdbc.JndiDataSourceAutoConfiguration

@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore({ XADataSourceAutoConfiguration.class, DataSourceAutoConfiguration.class })
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
@ConditionalOnProperty(prefix = "spring.datasource", name = "jndi-name")
@EnableConfigurationProperties(DataSourceProperties.class)
public class JndiDataSourceAutoConfiguration {

    @Bean(destroyMethod = "")
    @ConditionalOnMissingBean
    public DataSource dataSource(DataSourceProperties properties, ApplicationContext context) {
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource(properties.getJndiName());
        excludeMBeanIfNecessary(dataSource, "dataSource", context);
        return dataSource;
    }

    private void excludeMBeanIfNecessary(Object candidate, String beanName, ApplicationContext context) {
        for (MBeanExporter mbeanExporter : context.getBeansOfType(MBeanExporter.class).values()) {
            if (JmxUtils.isMBean(candidate.getClass())) {
                mbeanExporter.addExcludedBean(beanName);
            }
        }
    }

}


This content originally appeared on DEV Community and was authored by Salad Lam


Print Share Comment Cite Upload Translate Updates
APA

Salad Lam | Sciencx (2024-10-21T20:31:29+00:00) Quick look of JNDI (Java Naming and Directory Interface). Retrieved from https://www.scien.cx/2024/10/21/quick-look-of-jndi-java-naming-and-directory-interface/

MLA
" » Quick look of JNDI (Java Naming and Directory Interface)." Salad Lam | Sciencx - Monday October 21, 2024, https://www.scien.cx/2024/10/21/quick-look-of-jndi-java-naming-and-directory-interface/
HARVARD
Salad Lam | Sciencx Monday October 21, 2024 » Quick look of JNDI (Java Naming and Directory Interface)., viewed ,<https://www.scien.cx/2024/10/21/quick-look-of-jndi-java-naming-and-directory-interface/>
VANCOUVER
Salad Lam | Sciencx - » Quick look of JNDI (Java Naming and Directory Interface). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/10/21/quick-look-of-jndi-java-naming-and-directory-interface/
CHICAGO
" » Quick look of JNDI (Java Naming and Directory Interface)." Salad Lam | Sciencx - Accessed . https://www.scien.cx/2024/10/21/quick-look-of-jndi-java-naming-and-directory-interface/
IEEE
" » Quick look of JNDI (Java Naming and Directory Interface)." Salad Lam | Sciencx [Online]. Available: https://www.scien.cx/2024/10/21/quick-look-of-jndi-java-naming-and-directory-interface/. [Accessed: ]
rf:citation
» Quick look of JNDI (Java Naming and Directory Interface) | Salad Lam | Sciencx | https://www.scien.cx/2024/10/21/quick-look-of-jndi-java-naming-and-directory-interface/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.