JNDI是什么?SPI又是什么?

1,712次阅读
没有评论

共计 2784 个字符,预计需要花费 7 分钟才能阅读完成。

1.前言

时代的眼泪-JNDI,SPI?

2.JNDI

首先我们看下Oracle官方的描述

[success]The Java Naming and Directory Interface™ (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the Java™ programming language. It is defined to be independent of any specific directory service implementation. Thus a variety of directories -new, emerging, and already deployed can be accessed in a common way.[/success]

读完这句话,有点云里雾里的,好像和代码的解耦有点关系。毕竟是个提案,最终还是要结合实现来理解,看看它的架构图

 

JNDI是什么?SPI又是什么?

[success]The JNDI architecture consists of an API and a service provider interface (SPI). Java applications use the JNDI API to access a variety of naming and directory services. The SPI enables a variety of naming and directory services to be plugged in transparently, thereby allowing the Java application using the JNDI API to access their services. [/success]

到这里大致可以知道JNDI主要由API和SPI两部分构成,java应用调用JNDI API,JNDI API通过Naming Manager(绑定关系的载体)去调用SPI,SPI的具体实现通常是由第三方去实现的。在这个过程中API调用者其实是感知不到SPI的具体实现的,换言之,调用者只关心我能得到什么,而不在意结果如何而来的。The Java Naming and Directory Interface™ (JNDI),从名称我们也可以发现是一种基于命名和目录的抽象。举个例子,访问网站时,在没有DNS时,我们需要输入网站的IP地址才可以访问,有了DNS,我们可能只需要输入***.com之类的域名就可以访问了,这种现象我们可以称为Naming作用的结果,将IP和域名联系到一起称为绑定,DNS就是一种Naming Service。Directory就比较玄学了 JNDI是什么?SPI又是什么? ,它比命名多了些许属性,directory service = naming service + objects containing attributesJNDI是什么?SPI又是什么?

可以理解为Naming的目录,可能会包含更多的属性,比如大小,时间等,当然笔者水平有限,理解可能偏差,这里附上维基百科

In computing, a directory service or name service maps the names of network resources to their respective network addresses. It is a shared information infrastructure for locating, managing, administering and organizing everyday items and network resources, which can include volumes, folders, files, printers, users, groups, devices, telephone numbers and other objects. A directory service is a critical component of a network operating system. A directory server or name server is a server which provides such a service. Each resource on the network is considered an object by the directory server. Information about a particular resource is stored as a collection of attributes associated with that resource or object.

A directory service defines a namespace for the network. The namespace is used to assign a name (unique identifier) to each of the objects. Directories typically have a set of rules determining how network resources are named and identified, which usually includes a requirement that the identifiers be unique and unambiguous. When using a directory service, a user does not have to remember the physical address of a network resource; providing a name locates the resource. Some directory services include access control provisions, limiting the availability of directory information to authorized users.

3.SPI

SPI全称Service Provider Interface,是一种动态加载服务的功能。虽然JNDI使用场景很少了,但是SPI的使用还是很多的。这里不得不提ServiceLoader这个工具类JNDI是什么?SPI又是什么?

通过ServiceLoader可以很方便的加载服务实现,可以有效的降低代码的耦合性,提高灵活性,比如java.sql.Driver的实现,javax.validation.spi.ValidationProvider的实现等。阅读过springboot源码的小伙伴,会发现spring.factories也在类似的目录下 :biggrin:

4.小结

本文简单介绍JNDI和SPI,JNDI虽然是上个时代的产物,但是还是要有所了解的;目前对轻量化容器化的Java应用其实扮演的角色很少了,在JDBC,EJB,JMS方面仍然使用着。

正文完
 
mysteriousman
版权声明:本站原创文章,由 mysteriousman 2022-05-05发表,共计2784字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)