JNDI的目的是什么? [英] What is the purpose of JNDI

查看:120
本文介绍了JNDI的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能,您如何实现JNDI的使用,并举例说明?

How can you realize the usage of JNDI , with an example if possible?

推荐答案

JNDI是Java命名和目录接口。它用于分离应用程序开发人员和应用程序部署者的关注点。当您编写依赖于数据库的应用程序时,您不必担心用于连接到该数据库的用户名或密码。 JNDI允许开发人员为数据库命名,并依赖部署者将该名称映射到数据库的实际实例。

JNDI is the Java Naming and Directory Interface. It's used to separate the concerns of the application developer and 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.

例如,如果你'重新编写在Java EE容器中运行的代码,您可以编写此代码来获取JNDI名称为Database的数据源:

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


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不限于数据库(JDBC);各种服务都可以命名。有关详细信息,请查看有关该主题的 Sun教程

JNDI is not restricted to databases (JDBC); all sorts of services can be given names. For more details, you should check out the Sun tutorial on the subject.

这篇关于JNDI的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆