Home > Java > javaTutorial > body text

Quick look of JNDI (Java Naming and Directory Interface)

Patricia Arquette
Release: 2024-10-22 06:12:02
Original
720 people have browsed it

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 of the application server are placed under "java:comp/env" prefix. Assume that a data source is on

java:/comp/env/jdbc/db1
Copy after login

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");
Copy after login

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");
Copy after login

Spring boot integration

Just add a line into application.properties

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

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);
            }
        }
    }

}
Copy after login

The above is the detailed content of Quick look of JNDI (Java Naming and Directory Interface). For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!