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

发布于 2024-10-06 09:28:31 字数 1701 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

街角卖回忆 2024-10-13 09:28:31

什么是 JNDI?

它代表 Java 命名和目录接口

它的基本用途是什么?

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

什么时候使用?

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

这有几个优点:

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

What is JNDI ?

It stands for Java Naming and Directory Interface.

What is its basic use?

JNDI allows distributed applications to look up services in an abstract, resource-independent way.

When it is used?

The most common use case is to set up a database connection pool on a Java EE application server. Any application that's deployed on that server can gain access to the connections they need using the JNDI name java:comp/env/FooBarPool without having to know the details about the connection.

This has several advantages:

  1. If you have a deployment sequence where apps move from devl->int->test->prod environments, you can use the same JNDI name in each environment and hide the actual database being used. Applications don't have to change as they migrate between environments.
  2. You can minimize the number of folks who need to know the credentials for accessing a production database. Only the Java EE app server needs to know if you use JNDI.
缘字诀 2024-10-13 09:28:31

什么是 JNDI?

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

它的基本用途是什么?

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

在此处输入图像描述

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

  1. 轻量级目录访问协议 (LDAP)
  2. 公共对象请求代理架构 (CORBA) 公共对象服务 (COS) 名称服务
  3. Java 远程方法调用 (RMI)注册表

因此,基本上您创建对象并将它们注册到目录服务上,稍后您可以对其进行查找和执行操作。

What is JNDI ?

The Java Naming and Directory InterfaceTM (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the JavaTM 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.

What is its basic use?

Most of it is covered in the above answer but I would like to provide architecture here so that above will make more sense.

enter image description here

To use the JNDI, you must have the JNDI classes and one or more service providers. The Java 2 SDK, v1.3 includes three service providers for the following naming/directory services:

  1. Lightweight Directory Access Protocol (LDAP)
  2. Common Object Request Broker Architecture (CORBA) Common Object Services (COS) name service
  3. Java Remote Method Invocation (RMI) Registry

So basically you create objects and register them on the directory services which you can later do lookup and execute operation on.

究竟谁懂我的在乎 2024-10-13 09:28:31

通俗地说,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 in layman's terms is basically an Interface for being able to get instances of internal/External resources such as

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

or any other type defined by a JCA resource adapter.
It provides a syntax in being able to create access whether they are internal or external. i.e (comp/env in this instance means where component/environment, there are lots of other syntax):

jndiContext.lookup("java:comp/env/persistence/customerDB");
夕色琉璃 2024-10-13 09:28:31

JNDI 概述

JNDI是Java中指定的API
提供命名和
目录功能
用 Java 编写的应用程序
编程语言。它被设计
特别是对于Java平台使用
Java 的对象模型。使用 JNDI,
基于Java技术的应用程序
可以存储和检索命名的Java
任何类型的对象。此外,JNDI
提供了执行方法
标准目录操作,例如
将属性与对象关联起来
并使用它们来搜索对象
属性。

JNDI 的定义也独立于
任何特定的命名或目录
服务实施。它使
应用程序访问不同的,
可能有多个,命名和
使用通用 API 的目录服务。
不同的命名和目录服务
提供商可以无缝插入
这个通用 API 的背后。这使得
基于Java技术的应用程序
利用信息
各种现有的命名和
目录服务,例如 LDAP、NDS、
DNS 和 NIS(YP),以及启用
共存的应用程序
遗留软件和系统。

使用JNDI作为工具,您可以构建
新的强大且便携的应用程序
这不仅利用了Java的
对象模型,但也是
与环境良好融合
它们部署在其中。

参考

JNDI Overview

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.

Reference

青萝楚歌 2024-10-13 09:28:31

什么是 JNDI?

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

它的基本用途是什么?

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

何时使用?

您可以使用JNDI来执行命名操作,包括读取操作和更新命名空间的操作。 此处描述了以下操作。

What is JNDI ?

JNDI stands for Java Naming and Directory Interface. It comes standard with J2EE.

What is its basic use?

With this API, you can access many types of data, like objects, devices, files of naming and directory services, eg. it is used by EJB to find remote objects. JNDI is designed to provide a common interface to access existing services like DNS, NDS, LDAP, CORBA and RMI.

When it is used?

You can use the JNDI to perform naming operations, including read operations and operations for updating the namespace. The following operations are described here.

梦一生花开无言 2024-10-13 09:28:31

我将用一个例子来解释如何使用 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。

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

注意:加密的SecurityDomain,我们需要在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>

这是用例之一。希望它能澄清。

I will use one example to explain how JNDI can be used to configure database without any application developer knowing username and password of the database.

1) We have configured the data source in JBoss server's standalone-full.xml. Additionally, we can configure pool details also.

 <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>

enter image description here

Now, this jndi-name and its associated datasource object will be available for our application.application.

2) We can retrieve this datasource object using JndiDataSourceLookup class.

enter image description here

Spring will instantiate the datasource bean, after we provide the jndi-name.

Now, we can change the pool size, user name or password as per our environment or requirement, but it will not impact the application.

Note : encryptedSecurityDomain, we need to configure it separately in JBoss server like

<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>

This is one of the use cases. Hope it clarifies.

我的影子我的梦 2024-10-13 09:28:31

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

什么是 JNDI

它是一个 API,用于提供对目录服务的访问,即服务名称(字符串)与对象、远程对象引用或简单数据的映射。这就是所谓的
绑定。 绑定集称为上下文。应用程序使用JNDI接口来访问资源。

简单来说,它就像一个哈希图,其中包含字符串键和代表网络资源的对象值。

JNDI解决什么问题

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

例如,如果某个资源已被重新定位到另一台具有另一个 IP 地址的服务器上,则所有使用该资源的应用程序都必须使用此新信息进行更新。对于 JNDI,这是不必要的。只需要更新相应的资源绑定。应用程序仍然可以通过其名称访问它,并且重定位是透明的。

The best explanation to me is given here

What is JNDI

It is an API to providing access to a directory service, that is, a service mapping name (strings) with objects, reference to remote objects or simple data. This is called
binding. The set of bindings is called the context. Applications use the JNDI interface to access resources.

To put it very simply, it is like a hashmap with a String key and Object values representing resources on the web.

What Issues Does JNDI Solve

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.

权谋诡计 2024-10-13 09:28:31

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

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

A naming service associates names with objects and finds objects based on their given names.(RMI registry is a good example of a naming service.) JNDI provides a common interface to many existing naming services, such as LDAP, DNS.

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.

倒带 2024-10-13 09:28:31

我只是好奇为什么官方文档被如此忽略其中已经详细阐述了细节。

但如果您想了解这些案例,请参阅 duffymo 的回答

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

及其架构

在此处输入图像描述

通常如何使用它< /a>.

I am just curious why the official docs are so ignored which elaborate the details meticulously already.

But if you'd like to understand the cases, please refer to duffymo's answer.

The Java Naming and Directory InterfaceTM (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the JavaTM 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.

And its architecture

enter image description here

And normally how you use it.

不如归去 2024-10-13 09:28:31

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

虽然 JNDI 在 Spring Boot 等轻量级容器化 Java 应用程序中发挥的作用较小,但还有其他用途。仍然使用 JNDI 的三种 Java 技术是 JDBC、EJB 和 JMS。所有这些在 Java 企业应用程序中都有广泛的用途。

例如,单独的 DevOps 团队可以管理环境变量,例如所有环境中敏感数据库连接的用户名和密码。可以在 Web 应用程序容器中创建 JNDI 资源,并将 JNDI 用作适用于所有环境的一致抽象层。

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

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

The Java Naming and Directory InterfaceTM (JNDI) is an application programming interface (API) that provides naming and directory functionality to applications written using the JavaTM 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.

While JNDI plays less of a role in lightweight, containerized Java applications such as Spring Boot, there are other uses. Three Java technologies that still use JNDI are JDBC, EJB, and JMS. All have a wide array of uses across Java enterprise applications.

For example, a separate DevOps team may manage environment variables such as username and password for a sensitive database connection in all environments. A JNDI resource can be created in the web application container, with JNDI used as a layer of consistent abstraction that works in all environments.

This setup allows developers to create and control a local definition for development purposes while connecting to sensitive resources in a production environment through the same JNDI name.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文