how to put(bind) object to jndi in spring declaratively?

后端 未结 5 1704
抹茶落季
抹茶落季 2020-12-06 06:46

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

5条回答
  •  鱼传尺愫
    2020-12-06 07:44

    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.

提交回复
热议问题