1. What is Jndi?
JNDI (Java naming and Directory Interface) is a standard extension of the Java platform, providing a set of interfaces, classes, and concepts about namespaces. Like many other Java technologies, Jdni is a provider-based technology that exposes an API and a service Provisioning interface (SPI). This means that any name-based technology can provide services through Jndi, as long as Jndi supports this technology. The technologies currently supported by Jndi include LDAP, CORBA Common Object Service (COS) name Services, RMI, NDS, DNS, Windows registry, and so on.
Many of the EE technologies, including EJBS, rely on Jndi to organize and locate entities. The can be interpreted as a technique of bundling objects and names ( like a dictionary type, with key and value ) , and object factories are responsible for producing objects that are bound together with unique names. An external resource can obtain a reference to an object by name.
2. Why use JDI?
We can do it from the "What if we don't use Jndi ?" What will we do after using Jndi? To compare the two issues, consider the following example (example of a database connection):
1) in the case where Jndi is not applicable:
Connection conn=null;
try {
class.forname ("Com.mysql.jdbc.Driver",
true, Thread.CurrentThread (). Getcontextclassloader ());
conn=drivermanager.getconnection ("Jdbc:mysql://mydbserver?user=qingfeng&password=mingyue");
......
conn.close ();
}
catch (Exception e) {
e.printstacktrace ();
}
finally {
if (conn!=null) {
try {
conn.close ();
} catch (SQLException e) {}
}
}
2) in the case of Jndi use:
first configure the data source:
<?xml version= "1.0" encoding= "UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>mysqlds </jndi-name>
<CONNECTION-URL>JDBC:MYSQL://LOCALHOST:3306/LW </connection-url>
<driver-class>com.mysql.jdbc.driver </driver-class>
<user-name>root </user-name>
<password>rootpassword </password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.mysqlexceptionsorter </ Exception-sorter-class-name>
<metadata>
<type-mapping>mysql </type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
to reference a data source in a program:
Connection Conn=null;
try {
Context ctx=new InitialContext ();
Object datasourceref=ctx.lookup ("Java:mysqlds");//reference Data source
DataSource ds= (DataSource) datasourceref;
conn=ds.getconnection ();
......
C.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
finally {
if (conn!=null) {
try {
Conn.close ();
} catch (SQLException e) {}
}
}
in comparison with the above two cases can be obtained: using Jndi after the program can not care about the specific JDBC parameters. After the system deployment, if the relevant parameters of the database change, only need to reconfigure Mysql-ds.xml to modify the JDBC parameters, As long as the name of the data source is guaranteed to be the same, the program source code does not need to be modified. This shows that Jndi avoids tight coupling between the program and the database, making the application easier to configure and easy to deploy.
3. How do I use Jndi?
Providing the context interface in the Javax.naming package provides two useful methods: <1> void Bind (String name, Object object) binds the name to the object. All intermediate contexts and target contexts (specified by all components other than the final atomic component of the name) must already exist. <2>object Lookup (String name) retrieves the specified object. If name is empty, a new instance of this context is returned (the instance represents the same naming context as this context, but its environment can be modified independently and can be accessed concurrently).
Example: storing and retrieving objects from the Jndi namespace
- Import javax.naming.*;
- public void Createname() throws Namingexception {
- Context context= new InitialContext ();
- Context.bind ("/config/applicationname", "MyApp");
- }
- Public String GetName() throws Namingexception {
- Context context= new InitialContext ();
- Return (String) context.lookup ("/config/applicationname");
- }
The jndi of the Java EE