# 命名包

> 原文： [https://docs.oracle.com/javase/tutorial/jndi/overview/naming.html](https://docs.oracle.com/javase/tutorial/jndi/overview/naming.html)

[`javax.naming` ](https://docs.oracle.com/javase/8/docs/api/javax/naming/package-summary.html)包中包含用于访问命名服务的类和接口。

## 上下文

`javax.naming` 包定义了 [`Context`](https://docs.oracle.com/javase/8/docs/api/javax/naming/Context.html)接口，它是查找，绑定/解除绑定，重命名对象以及创建和销毁子上下文的核心接口。

Lookup

The most commonly used operation is [`lookup()`](https://docs.oracle.com/javase/8/docs/api/javax/naming/Context.html#lookup-javax.naming.Name-). You supply `lookup()` the name of the object you want to look up, and it returns the object bound to that name.

Bindings

[`listBindings()`](https://docs.oracle.com/javase/8/docs/api/javax/naming/Context.html#listBindings-javax.naming.Name-) returns an enumeration of name-to-object bindings. A binding is a tuple containing the name of the bound object, the name of the object's class, and the object itself.

List

[`list()`](https://docs.oracle.com/javase/8/docs/api/javax/naming/Context.html#list-javax.naming.Name-) is similar to `listBindings()`, except that it returns an enumeration of names containing an object's name and the name of the object's class. `list()` is useful for applications such as browsers that want to discover information about the objects bound within a context but that don't need all of the actual objects. Although `listBindings()` provides all of the same information, it is potentially a much more expensive operation.

Name

`Name` is an interface that represents a generic name--an ordered sequence of zero or more components. The Naming Systems use this interface to define the names that follow its conventions as described in the [Naming and Directory Concepts](../concepts/index.html) lesson.

References

Objects are stored in naming and directory services in different ways. A reference might be a very compact representation of an object.

JNDI 定义 [`参考`](https://docs.oracle.com/javase/8/docs/api/javax/naming/Reference.html) 类来表示参考。引用包含有关如何构造对象副本的信息。 JNDI 将尝试将从目录中查找的引用转换为它们所代表的 Java 对象，以便 JNDI 客户端错误地认为目录中存储的内容是 Java 对象。

## 初始背景

在 JNDI 中，所有命名和目录操作都是相对于上下文执行的。没有绝对的根源。因此，JNDI 定义了 [`InitialContext`](https://docs.oracle.com/javase/8/docs/api/javax/naming/InitialContext.html)，它为命名和目录操作提供了一个起点。一旦有了初始上下文，就可以使用它来查找其他上下文和对象。

## 例外

JNDI 定义了在执行命名和目录操作过程中可以抛出的异常的类层次结构。该类层次结构的根是 [`NamingException`](https://docs.oracle.com/javase/8/docs/api/javax/naming/NamingException.html)。对处理特定异常感兴趣的程序可以捕获异常的相应子类。否则，他们应该抓住 `NamingException` 。