gpt4 book ai didi

java - JNDI 说明 : JNDI API, JNDI SPI,服务提供商

转载 作者:行者123 更新时间:2023-11-30 08:12:18 24 4
gpt4 key购买 nike

我是 JNDI 的新手,我阅读了 oracle 的在线资料:

http://docs.oracle.com/javase/jndi/tutorial/getStarted/overview/index.html

它说 JNDI 有两个 API:

1) JNDI API
2) JNDI SPI

此外,它说要使用 JNDI,我们应该有 JNDI 类和服务提供者。

据我了解,服务提供者是实际的资源(命名或目录),例如LDAP 或 DNS(我的理解是否正确)?

我有以下疑问:

a) JNDI API:我们编写应用程序并使用 JNDI API 进行查找等。现在,谁来实现 JNDI API?它们本身是否完整实现,即由 JDK 提供者自己或服务提供者实现?

b) JNDI SPI:它到底是什么? JNDI SPI 是否特定于服务,例如LDAP 服务器?谁提供 JNDI SPI 的实现。仅供引用,我看到了 javax.naming.spi(以及其他)的源代码,我看到有一些接口(interface)和一些类。这些 JNDI SPI 是否在应用程序端使用(比如如果我正在编写一个简单的应用程序来从 LDAP 进行查找,那么这个 jar 是否在应用程序中)

非常感谢任何帮助。

编辑:

这是一个简单的 JNDI 程序。

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JNDIExample {

public static void main(String s[]) {

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");

// Is "com.sun.jndi.fscontext.RefFSContextFactory" the SPI API?
// What exactly is this?
Context ctx = new InitialContext(env);
try {
// Create the initial context
Context ctx = new InitialContext(env);

// Look up an object
Object obj = ctx.lookup(name);

// Print it
System.out.println(name + " is bound to: " + obj);

} catch (NamingException e) {
System.err.println("Problem looking up " + name + ": " + e);
}
}
}

对于上面的例子,我有以下疑问:

  1. 在上面的例子中,我们主要使用javax.naming.* 东西;谁实现它们?

  2. SPI 在哪里参与其中?

最佳答案

a) JNDI API: We write application and use JNDI API's to do lookup etc. Now, who does implement JNDI API? Are they complete implementation in itself i.e implemented by JDK providers themselves or by service providers?

由已注册 ObjectFactory 的人提供。 在 JRE 应用程序中,这可能不会超出 JRE 本身。在 Servlet 或 J2EE 容器中,它肯定会扩展以包括容器本身,用于 java:comp 资源,并且可能还有 Web 应用程序本身。

b) JNDI SPI: what exactly is it?

它是服务提供者必须实现的服务提供者接口(interface)。

Are JNDI SPI specific to a service e.g. LDAP server?

是的。

Who provides implementations of JNDI SPI.

几乎完全是 JRE 本身。

Are these JNDI SPI's used in the application side

它们可以,至少就 ObjectFactory 而言, 但这并不常见。

(like If i am writing a simple application to do lookup from LDAP, so would this jar be in application)

没有。

编辑关于您的新问题:

In this above example we are mainly using javax.naming.* stuff; who implements them?

JRE,特别是您指定的工厂类及其 friend 。

  1. Where is the SPI involved in this?

工厂类和 friend 实现了 SPI。

关于java - JNDI 说明 : JNDI API, JNDI SPI,服务提供商,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30616254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com