The jndi of the Java EE

Source: Internet
Author: User

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
    1. Import javax.naming.*;
    2. public void Createname() throws Namingexception {
    3. Context context= new InitialContext ();
    4. Context.bind ("/config/applicationname", "MyApp");
    5. }
    6. Public String GetName() throws Namingexception {
    7. Context context= new InitialContext ();
    8. Return (String) context.lookup ("/config/applicationname");
    9. }    

The jndi of the Java EE

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.