JNDI of J2EE, j2eejndi
1. What is JNDI?
JNDI (Java Naming and Directory Interface) is a standard extension of the Java platform. It provides a set of interfaces, classes, and namespaces. Like many other Java technologies, JDNI is a provider-based technology that exposes an API and a service supply interface (SPI ). This means that any name-based technology can provide services through JNDI, As long as JNDI supports this technology. Currently, the technology supported by JNDI includes LDAP, CORBA Common Object Service (COS) Name Service, RMI, NDS, DNS, and Windows registry.
Many J2EE technologies, including EJB, rely on JNDI to organize and locate entities.It can be understood as a technology that binds objects and names (It is like a dictionary with keys and values.)The object factory is responsible for producing the objects. These objects are associated with unique names. external resources can obtain references of an object by name.
2. Why use JDI?
What can we do from "what if we don't need JNDI? What will we do after using JNDI? "For comparison of the two questions, refer to the following example (using the database connection example ):
1) 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) When using JNDI:
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>
Reference the data source in a program:
Connection conn = null;
Try {
Context ctx = new InitialContext ();
Object datasourceRef = ctx. lookup ("java: MySqlDS"); // reference the 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 ){}
}
}
The comparison between the above two cases shows that the program after using JNDI does not need to care about the specific JDBC parameters. After the system deployment, if the database parameters change, you only need to reconfigure the mysql-ds.xml to modify the JDBC parameters, as long as the data source name remains unchanged, then the program source code does not need to be modified. It can be seen that JNDI avoids the tight coupling between the program and the database, making the application easier to configure and deploy.
3. How to Use JNDI?
The Context interface is provided in the javax. naming package and two useful methods are provided: <1> void bind (String name, Object object) binds the name to the Object. All intermediate context and target context (specified by all components other than the final atomic component of the name) must exist. <2> Object lookup (String name): retrieves the specified Object. If the name is blank, a new instance of the context is returned (the instance indicates the same naming context as the context, but its environment can be modified independently and can be accessed concurrently ).
For example, the following example stores and retrieves objects from the JNDI namespace.