Bo Sandevind
Bo Sandevind

Reputation: 41

Maven3 won't process spring annotations?

I have a Spring3.1/Hibernate3 project where the app-context is built by annotating classes + component scanning. Everything works fine when I build it with eclipse, but having built it with maven it seems the annotations are overlooked (nothing is injected, services are null, etc).

We have the compiler plugin as shown below:

<artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
        <source>1.5</source>
        <target>1.5</target>
    </configuration>

The "main" spring application context is loaded via contextloaderlistener from one of the associated web-projects (services/daos etc resides in a separate Common-project") To avoid instantiating one context for each web-project (we have a few, yes one can question that but..) we use the tecnique detailed in this blog: http://blog.springsource.org/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/

This is ripped from the web-xml instantiating the context:

    <listener>
    <display-name>ContextLoaderListener</display-name>
    <listener-  class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:beanRefContext.xml</param-value> 
</context-param>

    <context-param>
        <param-name>parentContextKey</param-name>
        <param-value>common.context</param-value>
    </context-param>

    <context-param>
    <param-name>webAppRootKey</param-name>
    <param-value>SpringHibernate</param-value>
    </context-param> 

BeanRefContext which lies in the Common-project looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

    <bean id="common.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg>
            <list>
                <value>appContext.xml</value>
            </list>
        </constructor-arg>
    </bean>

</beans>

And this in turns starts and ordinary ClassPathXmlApplicationContext with annotation-config and component scan definitions:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/context http://www.springframework.org /schema/context/spring-context-3.0.xsd">

        <context:annotation-config/>

    <import resource="persistenceContext.xml" />
    <import resource="businessContext.xml" />

    <context:component-scan base-package="com.XX.supplierportal.common.spring">
        <context:exclude-filter type="regex"    expression="com.XX.supplierportal.common.spring.*Test.java"/>
    </context:component-scan>
    <context:component-scan base-package="com.XX.supplierportal.userdirectory.incoming"/>

</beans>

And as stated above, everything preforms amiably in eclipse instantiating and sharing ONE common-applicationContext containing services etc among a number of web-projects, building it with maven however the contexts gets instantiated but it seems annotations are not read.

Anyone have a clue as to what is missing?

Upvotes: 2

Views: 315

Answers (2)

Bo Sandevind
Bo Sandevind

Reputation: 41

Having worked with this for a while, we saw it was the javax.annotation.Resource annotation that wasn't working and we finally realized that the maven build lacked the jsr250-api.jar (bonk self)

Upvotes: 1

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298898

I'm pretty sure this has nothing to do with maven, but with the spring configuration.

How do you instantiate the Spring Container? Which ApplicationContext implementation are you using? Please show the code that starts Spring and at least parts of your Spring XML.

Upvotes: 0

Related Questions