Java : JBoss - Spring integration
This page last changed on Apr 07, 2009 by Kees de Kooter
The Goal
Being able to use spring managed beans inside a JBoss application server, without introducing JBoss dependencies in the spring module.
Environment
- JBoss Application Server 5.0.1.GA
- Spring 2.5.6
Bootstrapping spring context
I created an MBean to boot up the spring context.
public interface SpringBootstrapServiceMBean {
void create() throws Exception;
void start() throws Exception;
void stop();
void destroy();
}
public class SpringBootstrapService implements SpringBootstrapServiceMBean {
private Log log = LogFactory.getLog(getClass());
private GenericApplicationContext applicationContext;
@Override
public void start() throws Exception {
log.info("Starting spring application context");
if (!applicationContext.isActive()) {
applicationContext.refresh();
applicationContext.start();
}
}
@Override
public void stop() {
if (applicationContext.isActive()) {
applicationContext.close();
}
log.info("Stopped spring application context");
}
@Override
public void create() throws Exception {
log.info("Creating spring application context");
applicationContext = new GenericApplicationContext();
XmlBeanDefinitionReader xmlBeanDefinitionReader =
new XmlBeanDefinitionReader(applicationContext);
xmlBeanDefinitionReader.loadBeanDefinitions(
new ClassPathResource("application-context.xml"));
applicationContext.refresh();
applicationContext.registerShutdownHook();
}
@Override
public void destroy() {
if (applicationContext.isActive()) {
applicationContext.close();
applicationContext.destroy();
}
log.info("Destroyed spring application context");
}
}
The MBean is recognized by putting a file called spring-service.xml in META-INF.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server
PUBLIC "-//JBoss//DTD MBean Service 5.0//EN"
"http://www.jboss.org/j2ee/dtd/jboss-service_5_0.dtd">
<server>
<mbean code="com...SpringBootstrapService"
name="jboss.service:service=SpringBootstrapService">
</mbean>
</server>