什么是JNDI? 它的基本用途是什么? 什么时候使用?


当前回答

JNDI概述

JNDI is an API specified in Java technology that provides naming and directory functionality to applications written in the Java programming language. It is designed especially for the Java platform using Java's object model. Using JNDI, applications based on Java technology can store and retrieve named Java objects of any type. In addition, JNDI provides methods for performing standard directory operations, such as associating attributes with objects and searching for objects using their attributes. JNDI is also defined independent of any specific naming or directory service implementation. It enables applications to access different, possibly multiple, naming and directory services using a common API. Different naming and directory service providers can be plugged in seamlessly behind this common API. This enables Java technology-based applications to take advantage of information in a variety of existing naming and directory services, such as LDAP, NDS, DNS, and NIS(YP), as well as enabling the applications to coexist with legacy software and systems. Using JNDI as a tool, you can build new powerful and portable applications that not only take advantage of Java's object model but are also well-integrated with the environment in which they are deployed.

参考

其他回答

我将使用一个示例来说明如何在应用程序开发人员不知道数据库用户名和密码的情况下使用JNDI来配置数据库。

1)我们已经在JBoss服务器的standalone-full.xml中配置了数据源。此外,我们还可以配置池的详细信息。

 <datasource jta="false" jndi-name="java:/DEV.DS" pool-name="DEV" enabled="true" use-ccm="false">
                <connection-url>jdbc:oracle:thin:@<IP>:1521:DEV</connection-url>
                <driver-class>oracle.jdbc.OracleDriver</driver-class>
                <driver>oracle</driver>
                <security>
                    <user-name>usname</user-name>
                    <password>pass</password>
                    </security>
                    <security>

 <security-domain>encryptedSecurityDomain</security-domain>
                    </security>

                <validation>
                    <validate-on-match>false</validate-on-match>
                    <background-validation>false</background-validation>
                    <background-validation-millis>1</background-validation-millis>
                </validation>
                <statement>
                    <prepared-statement-cache-size>0</prepared-statement-cache-size>
                    <share-prepared-statements>false</share-prepared-statements>
                    <pool>
                        <min-pool-size>5</min-pool-size>
                        <max-pool-size>10</max-pool-size>
                    </pool>
                </statement>
            </datasource>

现在,这个jndi-name及其关联的数据源对象将对我们的application.application可用。

2)我们可以使用JndiDataSourceLookup类来检索这个数据源对象。

在我们提供jndi-name之后,Spring将实例化数据源bean。

现在,我们可以根据环境或需求更改池大小、用户名或密码,但这不会影响应用程序。

注意:encryptedSecurityDomain,我们需要在JBoss服务器中单独配置它

<security-domain name="encryptedSecurityDomain" cache-type="default">
                    <authentication>
                        <login-module code="org.picketbox.datasource.security.SecureIdentityLoginModule" flag="required">
                            <module-option name="username" value="<usernamefordb>"/>
                            <module-option name="password" value="894c8a6aegc8d028ce169c596d67afd0"/>
                        </login-module>
                    </authentication>
                </security-domain>

这是其中一个用例。希望它能澄清。

什么是JNDI ?

JNDI代表Java命名和目录接口。它是J2EE的标准。

它的基本用途是什么?

使用这个API,您可以访问许多类型的数据,如对象、设备、命名文件和目录服务。EJB使用它来查找远程对象。JNDI旨在提供一个公共接口来访问现有的服务,如DNS、NDS、LDAP、CORBA和RMI。

什么时候使用?

您可以使用JNDI执行命名操作,包括读取操作和更新名称空间的操作。此处介绍以下操作。

Java命名和目录接口(JNDI)是一个应用程序编程接口(API),为使用JavaTM编程语言编写的应用程序提供命名和目录功能。它被定义为独立于任何特定的目录服务实现。因此,可以以通用的方式访问各种目录——新的、正在出现的和已经部署的目录。

尽管JNDI在轻量级、容器化的Java应用程序(如Spring Boot)中扮演的角色较少,但还有其他用途。仍然使用JNDI的三种Java技术是JDBC、EJB和JMS。它们在Java企业应用程序中都有广泛的用途。

例如,一个独立的DevOps团队可能在所有环境中管理敏感数据库连接的用户名和密码等环境变量。JNDI资源可以在web应用程序容器中创建,JNDI被用作在所有环境中工作的一致抽象层。

这种设置允许开发人员为开发目的创建和控制本地定义,同时通过相同的JNDI名称连接到生产环境中的敏感资源。

参考: https://docs.oracle.com/javase/tutorial/jndi/overview/index.html

JNDI在外行的术语中基本上是一个接口,用于获得内部/外部资源的实例,例如

  javax.sql.DataSource, 
  javax.jms.Connection-Factory,
  javax.jms.QueueConnectionFactory,
  javax.jms.TopicConnectionFactory,
  javax.mail.Session, java.net.URL,
  javax.resource.cci.ConnectionFactory,

或由JCA资源适配器定义的任何其他类型。 它提供了一种能够创建访问的语法,无论访问是内部的还是外部的。例如(在这个例子中,comp/env的意思是组件/环境,有很多其他的语法):

jndiContext.lookup("java:comp/env/persistence/customerDB");

我只是好奇为什么官方文件如此被忽视,而这些文件已经一丝不苟地阐述了细节。

但如果你想了解这些案例,请参考duffymo的答案。

Java命名和目录接口(JNDI)是一个应用程序编程接口(API),为使用JavaTM编程语言编写的应用程序提供命名和目录功能。它被定义为独立于任何特定的目录服务实现。因此,可以以通用的方式访问各种目录——新的、正在出现的和已经部署的目录。

以及它的结构

以及通常如何使用它。