quote

martedì, dicembre 22, 2009

La neve, Spinea e Andrea

Enrico, scrivi qualcosa cribbio!
"Tornando da una seratina passata al lume di candela con la fata Monia, incontrai Andrea. Ma che ci fa Andrea da solo, alle 22, davanti ad un cassettone delle immondizie, in tuta da ginnastica? Era uscito a buttare la spazzatura ?"
Ma che ne so Enrico inventa!
(Testo della mail di Andrea...scusate ma sono troppo pigro ultimamente :) )


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.