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 (current version is 2.5.6) - (Why?).
But it's easy to write it youself:
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)) {
jndiTemplate.unbind(jndiName);
}
}
}
Add following fragment to spring configuration file:
<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"/>
</bean>
Don't forget to make your bean serializable by implementing
java.io.Serializable
interface.
Now we can lookup exported bean by adding to the spring config fil:
<jee:jndi-lookup id="myJndiBean" jndi-name="MyJNDIName" proxy-interface="com.example.IMyBean" lookup-on-startup="false"/>
That's all, folks!