Search code examples
javajndi

Understanding JNDI


JNDI is like a map on steroids right? I use a key to find references to objects. Also, what is InitialContext? I don't seem to get the idea.


Solution

  • Conceptually, JNDI is like System.getProperties() on steroids.

    System.getProperties() allows you to pass String parameters to your code from the command line. Similarly, JNDI allows you to configure arbitrary objects outside of your code (for example, in application server config files) and then use them in your code.

    In other words, it's an implementation of Service Locator pattern: your code obtains services configured by environment from the centeral registry.

    As usually with Service Locators, your code should have some entry point to access Service Locator. InitialContext is this entry point: you create InitialContext and then obtain required services from JNDI with lookup().