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


当前回答

什么是JNDI ?

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

它的基本用途是什么?

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

什么时候使用?

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

其他回答

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

对我来说最好的解释在这里

什么是JNDI

它是一个API,用于提供对目录服务的访问,即服务映射名称(字符串)与对象、对远程对象或简单数据的引用。这叫做 绑定。绑定的集合称为上下文。应用程序使用JNDI接口访问资源。 简单地说,它就像一个带有String键和Object值的hashmap,表示web上的资源。

JNDI解决了什么问题

Without JNDI, the location or access information of remote resources would have to be hard-coded in applications or made available in a configuration. Maintaining this information is quite tedious and error prone. If a resources has been relocated on another server, with another IP address, for example, all applications using this resource would have to be updated with this new information. With JNDI, this is not necessary. Only the corresponding resource binding has to be updated. Applications can still access it with its name and the relocation is transparent.

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.

参考

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

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

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

以及它的结构

以及通常如何使用它。

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

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