We have a plain standalone spring application and we need to put jdbc datasource in jndi. (we use jboss treecache and it need datasource to be in the jndi).
Some go
If the code is being executed outside a Servlet container, e.g. in a unit test, the JNDI context needs to be emulated. Otherwise you'll get the dreaded "Need to specify class name in environment ..." error.
SimpleNamingContextBuilder is better suited for that, than JndiTemplate:
public class JndiExporter implements InitializingBean {
private final SimpleNamingContextBuilder contextBuilder = new SimpleNamingContextBuilder();
private Map jndiMapping = null;
@Override
public void afterPropertiesSet() throws Exception {
for (Entry addToJndi : jndiMapping.entrySet()) {
contextBuilder.bind(addToJndi.getKey(), addToJndi.getValue());
}
contextBuilder.activate();
}
public void setJndiMapping(Map jndiMapping) {
this.jndiMapping = jndiMapping;
}
Do not overlook the "contextBuilder.activate();" line.