First, Jndi overview
JNDI (javanaming directory Interface)--java naming and directory interfaces , is a set of APIs that access the naming and directory services in a Java application. Provides developers with a common, unified interface for locating and accessing various naming and directory services, similar to JDBC, built on the abstraction layer. is one of the standards of the Java EE, all the Java EE containers must provide a JNDI service.
JNdi's life claim may be confusing, at first touching the specification that this is a directory to manipulate, in fact, you can interpret this directory as the Jndi to store the object in the format used, that is, Jndi stores the properties of an object in a directory way.
Second, naming services and directory services
Naming or directory services enable users to centrally store shared information, which makes such applications more coordinated and manageable. For example, you can store printer settings in a directory service for use by apps that are related to your printer.
The naming service is used unconsciously by all of us every day. An object in a named system can be a name in a DNS record, an EJB component in an application server (Enterprise JavaBeans Component) , user profile in LDAP (lightweightdirectory Access Protocol) .
The directory service is a natural extension of the naming service. The key difference between the two is that the object in the directory service can have attributes (for example, the user has an email address), and the object in the naming service has no attributes. Therefore, in the directory service, you can search for objects based on attributes.
Three, the common basic operation
1. Binding and release
1)voidbind (String sName,objectobject) binding: The process of associating a name with an object
2)void rebind(String sName,objectobject) rebinding: Used to rebind the object with an existing name. Rebind () is generally used instead of bind (), because rebind () does not have an exception when there is a duplicate name, and bind () reports an exception.
3)Voidunbind(String sName) Release: Used to release objects from the directory.
The application code is as follows:
Public Staticcontext Getinitialcontext () throws namingexception{ Environment env = new Environment (); Env.setproviderurl ("xxx"); Env.setsecurityprincipa ("system"); Env.setsecuritycredendtials ("WebLogic"); Contextcontext = Env.getinitialcontext (); Returncontext;} Get intial context Context ctx =getinitialcontext (); Create an object named band Bank MyBank = Newbank (); Object binding Ctx.bind ("Thebank", MyBank); Rebind the object to the Jndi tree ctx.rebind ("Thebank", MyBank); Unbind ctx.unbind ("Thebank"); Ctx.close ();
2. Find objects
Void Lookup (String Sname,object object) Lookup: Returns the total object of a directory
importjavax.naming.*;try{ Context IC = new context (); Objectobj; obj= ic.lookup ("javax.transaction.UserTransaction"); Usertransactionut = (usertransaction) obj; Ut.begin (); Ic.close ();} catch (Namingexceptione) {}
3. Renaming Objects
void Rename(String soldname,stringsnewname) Rename: The name used to modify the object name binding.
Ctx.rename ("Report.txt", "old_report.txt");
4. Get the list
namingenumerationlistbindings (stringsname) Checklist: Returns a list of manifests that are bound to the specified property name object in a specific context, which returns the name, class, and object itself, and is used for applications that need to actually manipulate the object.
Get a reference to the initial directory environment Context Cntxt = new InitialContext ();//Returns a list of manifest lists that are bound to the specified property name object in a specific context namingenumeration Namenumlist = Ctxt.listbinding ("Cntxtname");//loop lists all names, classes, and objects while (Namenumlist.hasmore ()) { binding BND = (binding) Namenumlist.next (); String sobjname = Bnd.getname (); String sclassname = Bnd.getclassname (); Get Object someobject objlocal = (someobject) bnd.getobject ();}
Iv. Summary
The idea of Jndi is to provide a common interface. The underlying name service timeframe can be of various types,andJndi masks the differences between the various name and directory service implementations through the SPI time-fence code.
Jndi can solve the problem of data sharing, distributed application and tight coupling, so it can be applied in some large-size software development, distributed system or in the project that pay more attention to later maintenance and upgrade.
"Analysis of the Java--jndi"