什么是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概述

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 ?

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

它的基本用途是什么?

上面的答案涵盖了大部分内容,但我想在这里提供架构,以便上面的内容更有意义。

要使用JNDI,您必须拥有JNDI类和一个或多个服务提供者。Java 2 SDK v1.3包含三个服务提供者,用于以下命名/目录服务:

轻量级目录访问协议(LDAP) CORBA (Common Object Request Broker Architecture)通用对象服务(COS)名称服务 Java远程方法调用注册表

所以基本上你创建对象并在目录服务上注册它们,然后你可以在目录服务上进行查找和执行操作。

什么是JNDI ?

它代表Java命名和目录接口。

它的基本用途是什么?

JNDI允许分布式应用程序以抽象的、独立于资源的方式查找服务。

什么时候使用?

最常见的用例是在Java EE应用服务器上设置数据库连接池。部署在该服务器上的任何应用程序都可以使用JNDI名称java:comp/env/FooBarPool访问所需的连接,而无需了解有关连接的详细信息。

这有几个优点:

如果你有一个应用程序从devl->int->test->prod环境迁移的部署序列,你可以在每个环境中使用相同的JNDI名称,并隐藏实际使用的数据库。应用程序在环境之间迁移时不必更改。 您可以尽量减少需要知道访问生产数据库的凭据的人数。只有Java EE应用服务器需要知道您是否使用JNDI。

命名服务将名称与对象关联起来,并根据对象的给定名称查找对象。(RMI注册表是命名服务的一个很好的例子。)JNDI为许多现有的命名服务(如LDAP、DNS)提供了一个公共接口。

如果没有JNDI,远程资源的位置或访问信息必须硬编码在应用程序中或在配置中可用。维护这些信息非常繁琐,而且容易出错。