I was surprised I'd never seen someone doing this before - use Spring to create and configure a JNDI context.
In ServiceMix we need to work inside a J2EE container such as when we run inside Geronimo to add JBI capability to the Apache J2EE stack. We also need to work embedded inside any Java SE platform or inside, say, Tomcat.
So as well as reusing any JNDI context available - such as for JDBC
DataSources etc - we need to be able to provide one when running
ServiceMix in stand alone mode for use by other JBI components.
There are a couple of JNDI implementations around such as the one in Apache Directory (which has lots of dependencies) and simple-jndi - which looks quite good but I couldn't get to work properly with hierarchial namespaces (probably pilot error).
But since most things we work with these days tends to use Spring's XML
configuration mechanism we kinda wanted to reuse its ability to nicely
configure objects using dependency injection as well as having a ton of
neat features (like AOP & interceptors, pooling, factory beans,
dependencies, declarative transactions etc). Plus once you become
proficient in one XML mechanism to configure POJOs, you don't wanna
have to use another one just for JNDI.
So we enhanced ActiveMQ's default
JNDI provider with a fairly simple Spring wrapper implementation and
hey presto we can configure JNDI using regular Spring XML
All thats required is to define this jndi.properties on the classpath
java.naming.factory.initial = org.servicemix.jbi.jndi.SpringInitialContextFactory
Then this jndi.xml can be placed on the classpath and hey presto - you've a configured JNDI context all configured using Spring.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<bean id="jndi" class="org.servicemix.jbi.jndi.DefaultContext">
<property name="database" value="jdbc:hsqldb:mem:pxe"/>
You can see more discussions and examples here