quote

giovedì, dicembre 17, 2009

Deploying Spring @Component annotated application in JBoss 5.1

I've spent the last few days fighting with jboss 5.1 trying to deploy a spring based web application using the Component annotation.
The application as opposed works using Tomcat 6.
Basically the spring context was not able to find the definition of SchedulerBo which is my Component annotated class.

package webscheduler.bo;

@Component
public class SchedulerBo {
...............................
}

This is the section of my spring configuration file which allows the spring context to auto-scan the schedulerBo.

<context:component-scan base-package="webscheduler.bo"/>

This is the exception I had.

19:17:09,706 ERROR [ContextListener] Error in base ContextListener.contextInitialized
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobNormal' defined in ServletContext resource [/WEB-INF/schedulerContext.xml]: Cannot resolve reference to bean 'schedulerBo' while setting bean property 'jobDataAsMap' with key [TypedStringValue: value [schedulerBo], target type [null]]; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'schedulerBo' is defined

............................................................

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'schedulerBo' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.
getBeanDefinition(DefaultListableBeanFactory.java:387)

This is the main section of my listener (ServletContextListener) class which I used to load the spring context(marked in red the row to change)

private WebApplicationContext applicationContext;
.....................................
config.add("WEB-INF/schedulerContext.xml");
XmlWebApplicationContext ctx = new XmlWebApplicationContext();
ctx.setServletContext(servletContext);
ctx.setConfigLocations(config.toArray(new String[config.size()]));
ctx.refresh();
applicationContext = ctx;

To sort out this problem you have to use org.jboss.spring.factory.VFSXmlWebApplicationContext instead of rg.springframework.web.context.support.XmlWebApplicationContext.
Therefore I changed this row in my listener.

XmlWebApplicationContext ctx = new org.jboss.spring.factory.VFSXmlWebApplicationContext();

Here the steeps to follow to solve the problem:

  1. Add this dependency to your pom, be sure to have the jboss repository in your pom.xml


    <dependency>
    <groupId>org.jboss.snowdrop</groupId>
    <artifactId>snowdrop-vfs</artifactId>
    <version>1.0.0.GA</version>
    <exclusions>
    <exclusion>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    </exclusion>
    <exclusion>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    </exclusion>
    <exclusion>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    </exclusion>
    <exclusion>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    </exclusion>
    </exclusions>
    </dependency>


  2. Using VFSXmlWebApplicationContext in your listener class in this way

    XmlWebApplicationContext ctx = new org.jboss.spring.factory.VFSXmlWebApplicationContext();

  3. run $ mvn clean install and deploy the new generated war into <JBOSS_HOME>/server/default/deploy


This is the link in JBoss JIRA which reports the bug and shows the solution.

1 commento:

Anonimo ha detto...

Upgrading to Spring 3.0 will also solve this problem.