How To Export Spring Managed Bean To JNDI

Jan 1, 2009

Sometimes it is necessary to export a spring managed bean to JNDI context. Here I want to show how do it.

In spring, there is a bean that provides a similar functionality for exporting to MBean server: MBeanExporter. Unfortunately, there is no standard JNDI bean exporter implementation in Spring Framework (current version is 2.5.6) - (Why?). But it’s easy to write it yourself


package com.example.spring.jndi.export;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jndi.JndiTemplate;

public class JndiExporter implements InitializingBean, DisposableBean {

    private String jndiName;
    private Object bean;
    private final JndiTemplate jndiTemplate = new JndiTemplate();

    public String getJndiName() {
        return jndiName;

    public void setJndiName(String jndiName) {
        this.jndiName = jndiName;

    public Object getBean() {
        return bean;

    public void setBean(Object bean) {
        this.bean = bean;

    public void afterPropertiesSet() throws Exception {
        jndiTemplate.bind(jndiName, bean);

    public void destroy() throws Exception {
        if (bean != null && jndiName != null && bean == jndiTemplate.lookup(jndiName)) {

Add following fragment to spring configuration (applicationContext.xml):

<bean id="myBean" class="com.example.MyBean"/>

<bean class="com.example.spring.jndi.export.JndiExporter">
    <property name="bean" ref="myBean" />
    <property name="jndiName" value="MyJNDIName"/>

Don’t forget to make your bean implementing interface. Now we can lookup exported bean by adding to the spring config file (applicationContext.xml):

<jee:jndi-lookup id="myJndiBean" jndi-name="MyJNDIName" proxy-interface="com.example.IMyBean" lookup-on-startup="false"/>

If you have another bean depending on JNDI resource you may try to load you that bean lazily by setting lazy-init="true". You may also try to set lazy-init="false" for the JndiExporter to ensure that the Exporter will be loaded before JNDI-dependent bean or play with "depends-on" bean attributes.

See Also

Configuring WS-Security for Axis 1.4 client

I was looking how to enable WS-Security features in the Axis client to my web service application. I have tested it with Axis 1.4 client.

logo   Never miss a story, subscribe to our newsletter