How can I store Java EE configuration parameters outside of an EAR or WAR?

See this question for reading properties file outside of the WAR file.

See this question for reading variable values from JNDI. I believe that this is the best solution. You can read a String variable with this code:

Context initialContext = new InitialContext();
String myvar = (String) initialContext.lookup("java:comp/env/myvar");

The above code will work on all containers. In Tomcat you declare the following in conf/server.xml:

<GlobalNamingResources ...>
  <Environment name="myvar" value="..."
         type="java.lang.String" override="false"/>
</GlobalNamingResources>

The above will create a global resource. It is also possible to define a resource in the context of application. In most containers the JNDI resources are available through a MBeans Management Console. Some of them offer a graphical interface to edit them. At most an application restart is needed, when a change is made.

How JNDI resources are defined and edited is container specific. It is the job of the configurator/administrator to apply the appropriate settings.

These are the benefits offered by JNDI:

  • You can define default values of the parameters in the WAR/EAR file.
  • Parameters are easily configurable at the container.
  • You don’t need to restart the container when you modify the value of a parameter.

Leave a Comment