Java JNDI的目的是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1350816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the purpose of JNDI
提问by Ajay
How can you realize the usage of JNDI , with an example if possible?
你如何实现 JNDI 的用法,如果可能的话,举个例子?
采纳答案by Simon Nickerson
JNDI is the Java Naming and Directory Interface. It's used to separate the concerns of the application developerand the application deployer. When you're writing an application which relies on a database, you shouldn't need to worry about the user name or password for connecting to that database. JNDI allows the developer to give a name to a database, and rely on the deployer to map that name to an actual instance of the database.
JNDI 是 Java 命名和目录接口。它用于分离应用程序开发人员和应用程序部署人员的关注点。当您编写依赖于数据库的应用程序时,您无需担心连接到该数据库的用户名或密码。JNDI 允许开发人员为数据库命名,并依靠部署人员将该名称映射到数据库的实际实例。
For example, if you're writing code that runs in a Java EE container, you can write this to get hold of the data source with JNDI name "Database":
例如,如果您正在编写在 Java EE 容器中运行的代码,您可以编写以下代码来获取 JNDI 名称为“Database”的数据源:
DataSource dataSource = null;
try
{
Context context = new InitialContext();
dataSource = (DataSource) context.lookup("Database");
}
catch (NamingException e)
{
// Couldn't find the data source: give up
}
Note there's nothing here about the database driver, or the user name, or the password. That is configured inside the container.
请注意,这里没有关于数据库驱动程序、用户名或密码的任何内容。那是在容器内部配置的。
JNDI is not restricted to databases (JDBC); all sorts of services can be given names. For more details, you should check out Oracle's tutorial.
JNDI 不限于数据库(JDBC);各种服务都可以命名。有关更多详细信息,您应该查看 Oracle 的教程。
回答by oxbow_lakes
JNDI is a very powerful mechanism for both organizing configuration information and discovering and listening to servicesvia using the EventContext
. In JNDI you can lookup and listen to anyobject (not just DataSource
s), assuming your JNDI service provider supports it.
JNDI是两个组织配置信息和一个非常强大的机制发现并听取服务通过使用EventContext
。在 JNDI 中,您可以查找和侦听任何对象(不仅仅是DataSource
s),假设您的 JNDI 服务提供者支持它。
Of course, the only issue is actually having a JNDI service provider; the great thing about this is that it surprisingly easy to roll your own. After all you can encode any Java instanceinto XML
using the JavaBeans XMLEncoder
and XMLDecoder
: you don't need to rely on running within an application server!
当然,唯一的问题实际上是拥有 JNDI 服务提供者;这样做的好处是,推出自己的产品非常容易。毕竟,您可以将任何 Java 实例编码为XML
使用 JavaBeansXMLEncoder
并且XMLDecoder
:您不需要依赖于在应用程序服务器中运行!
So what is the difference between this an having configuration files? Well, it can be much cleaner because all of your applications can get their configuration from the same place. If they need to share configuration information (e.g. database locations) then this can be defined once in JNDI. Suppose you moved database servers: you don't need to remember the gazillion config files with the location in it. You just go to the one place: JNDI.
那么这和有配置文件有什么区别呢?嗯,它可以更简洁,因为您的所有应用程序都可以从同一个地方获取它们的配置。如果他们需要共享配置信息(例如数据库位置),那么这可以在 JNDI 中定义一次。假设您移动了数据库服务器:您不需要记住包含位置的无数配置文件。您只需转到一个地方:JNDI。
回答by ilupper
JNDI allows the simplification of a resource construct into just a name. So, it's many details groupinto 1 for convenience/security/etc. (aka abstraction layer)
JNDI 允许将资源构造简化为一个名称。因此,为了方便/安全/等,将许多细节分组为 1。(又名抽象层)
to realize:set up a properties list that corresponds to the predefined fields in the Jndi Context Interface. (these properties specify the settings for the jndi execution; but *not the search name)
实现:在Jndi Context Interface中设置一个对应预定义字段的属性列表。(这些属性指定 jndi 执行的设置;但 * 不是搜索名称)
Properties props = new Properties();
//field Context.INITIAL_CONTEXT_FACTORY => property name java.naming.factory.initial
//field Context.PROVIDER_URL => property name java.naming.provider.url
props.load(new FileInputStream("*properties file*")); //prop file in this case
Context ctx = new InitialContext(props);
Object o = ctx.lookup("*name of resource*");
ideally, a specialized function would exist to maintain a LDAP directory, DNS, etc, at your organization (so a unified single mapping set services all, reducing discrepancies)
理想情况下,将存在一个专门的功能来维护组织中的 LDAP 目录、DNS 等(因此统一的单一映射集可以为所有服务提供服务,从而减少差异)
List of JNDI Service Providers: https://www.ibm.com/support/knowledgecenter/en/SSVSD8_8.4.1/com.ibm.websphere.dtx.adapjndi.doc/concepts/c_jndi_JNDI_Service_Providers_.htm
JNDI 服务提供者列表:https: //www.ibm.com/support/knowledgecenter/en/SSVSD8_8.4.1/com.ibm.websphere.dtx.adapjndi.doc/concepts/c_jndi_JNDI_Service_Providers_.htm
回答by Ithar
JNDI is an API used to access the directory and naming services (i.e. the means by which names are associated with objects). The association of a name with an object is called a binding.
JNDI 是一种用于访问目录和命名服务(即名称与对象关联的方式)的 API。名称与对象的关联称为绑定。
A basic example of a naming service is DNS which maps machine names to IP addresses.
命名服务的一个基本示例是 DNS,它将机器名称映射到 IP 地址。
Using JNDI, applications can store and retrieve named Java objects of any type.
使用 JNDI,应用程序可以存储和检索任何类型的命名 Java 对象。
Within the context of java this can be used in configuration files where you don't want to hard-code environment specific variables.
在 java 的上下文中,这可以在您不想硬编码环境特定变量的配置文件中使用。
Spring Example:
弹簧示例:
Spring context file
弹簧上下文文件
<bean id="WSClientConfig" class="com.example.BaseClientConfigImpl">
<property name="protocol">
<jee:jndi-lookup jndi-name="java:comp/env/protocol" />
</property>
<property name="endpoint">
<jee:jndi-lookup jndi-name="java:comp/env/endpoint" />
</property>
<property name="requestPath">
<jee:jndi-lookup jndi-name="java:comp/env/requestPath" />
</property>
Tomcat context file
Tomcat 上下文文件
<Environment name="protocol" type="java.lang.String" value="https://"/>
<Environment name="endpoint" type="java.lang.String" value="172.0.0.1"/>
<Environment name="requestPath" type="java.lang.String" value="/path/to/service"/>