添加,替换或删除绑定
Context
interface包含用于adding,replacing和removing上下文中绑定的方法。
添加绑定
Context.bind()用于向上下文添加绑定。它接受对象名称和要绑定的对象作为参数。
在 continue 之前: 本课中的示例要求您对架构进行补充。您必须关闭 LDAP 服务器中的 Pattern 检查,或者将本教程随附的the schema添加到服务器。这两个任务通常由目录服务器的 管理 员执行。请参阅LDAP Setup类。
// Create the object to be bound
Fruit fruit = new Fruit("orange");
// Perform the bind
ctx.bind("cn=Favorite Fruit", fruit);
This example创建一个类Fruit的对象,并将其绑定到上下文ctx
中的名称"cn=Favorite Fruit"
。如果随后在ctx
中查找名称"cn=Favorite Fruit"
,则将获得fruit
对象。请注意,要编译Fruit
类,您需要FruitFactory类。
如果要两次运行此示例,则第二次try将失败,并带有NameAlreadyBoundException。这是因为名称"cn=Favorite Fruit"
已绑定。为了第二次try成功,您必须使用rebind()。
添加或替换绑定
rebind\(\)
用于添加或替换绑定。它接受与bind\(\)
相同的参数,但语义上是这样的:如果名称已绑定,则它将被取消绑定,而新给定的对象将被绑定。
// Create the object to be bound
Fruit fruit = new Fruit("lemon");
// Perform the bind
ctx.rebind("cn=Favorite Fruit", fruit);
运行this example时,它将替换bind()示例创建的绑定。
删除绑定
要删除绑定,请使用unbind()。
// Remove the binding
ctx.unbind("cn=Favorite Fruit");
This example运行时,将删除由bind()或rebind()示例创建的绑定。