10 März 2014

Use JNDI to Configure your Java EE App

There are a lot of people talking about Java EE configuration these days. But often they forget about JNDI (Java Naming and Directory Interface).

Configuration using JNDI has the advantage that you can use dependency injection. i.e.

@Resource(mappedName = "java:global/mystring")
private String mystring;

The configuration for example in JBosss EAP or Wildfly is pretty simple made in the standalone.xml (or domain.xml) file:

<subsystem xmlns="urn:jboss:domain:naming:1.4">
  <bindings>
    <simple name="java:global/mystring" 
            value="My String" 
            type="java.lang.String"/>
     </bindings>
     <remote-naming/>
</subsystem>

As you can see the type can be defined. If you need more complex objects simply use ObjectFactory http://docs.oracle.com/javase/7/docs/api/javax/naming/spi/ObjectFactory.html

Read more about JNDI in WildFly: https://docs.jboss.org/author/display/WFLY8/JNDI+Reference
and more about ObjectFactory: http://middlewaremagic.com/jboss/?p=1690

Keine Kommentare: