tangobee
tangobee

Reputation: 926

How to autowire class in Maven multi module Spring web app?

I am working on application which contains Maven multi module projects. When tryng to @Autowire a service class from another module I'm getting java.lang.NoClassDefFoundError: What makes this project unique is the dependency is between 2 web modules.

Parent Project


Pom.xml

<project>
    <groupId>com.test.simple-project</groupId>
    <artifactId>simple-parent</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>module-x</module>
        <module>module-y</module>
    </modules>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Module X


Pom.xml

<project>
    <artifactId>module-x</artifactId>
    <parent>
        <groupId>${project.groupId}</groupId>
        <artifactId>simple-parent</artifactId>
    </parent>
    <packaging>war</packaging>
     <dependencies>
        ...
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


package com.test.module-x.service;      
@Service("userService")
   public class UserServiceImpl implements UserService {
}

Module Y


Pom.xml

<project>
    <artifactId>module-y</artifactId>
    <parent>
        <groupId>${project.groupId}</groupId>
        <artifactId>simple-parent</artifactId>
        <version>${project.version}</version>
    </parent>
    <packaging>war</packaging>
     <dependencies>
        <dependency>
            <groupId>com.test.simpleproject</groupId>
            <artifactId>module-x</artifactId>
        </dependency>
        ...
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


component-scan in Y

<context:component-scan base-package="com.test.module-x.service" />


 @Controller
     public class SimpleController {

        @Autowired
        private UserService userService;
    }

Both modules are Spring mvc web projects (both pom.xml packaging is war)

Running the app throws java.lang.NoClassDefFoundError: com/test/module-x/service/UserService

java.lang.NoClassDefFoundError: Lcom/test/module-x/service/UserService;
    at java.lang.Class.getDeclaredFields0(Native Method)
        at java.lang.Class.privateGetDeclaredFields(Class.java:2570)
        at java.lang.Class.getDeclaredFields(Class.java:1903)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:324)
        at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:285)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:846)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:498)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
        at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
        at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
        at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
        at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)

Any ideas?

Upvotes: 4

Views: 6845

Answers (2)

manish
manish

Reputation: 20135

A WAR module with a dependency on another WAR module does not have access to classes generated by the dependency module. You will therefore have to provide the classes in module-x to those in module-y. There are two ways to do it.

Option 1: Cleaner design


Abstract out common classes into a separate JAR module and declare a dependency from both module-x and module-y to this module.

Option 2: Get access to classes declared in module-x from classes in module-y


First, configure the maven-war-plugin in module-x to package the classes as a separate JAR file.

<project>
  <artifactId>module-x</artifactId>
  <packaging>war</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <attachClasses>true</attachClasses>
          <classesClassifier>classes</classesClassifier>
        </configuration>
      </plugin>
    </plugins>
  </build>

Now, declare a dependency on module-x in module-y:

<project>
  <artifactId>module-y</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module-x</artifactId>
      <version>${project.version}</version>
      <classifier>classes</classifier>
    </dependency>
  </dependencies>
</project>

Upvotes: 3

iullianr
iullianr

Reputation: 1294

You have to define, in the pom.xml file associated to the Module Y a dependency on the output war file of the Module X. It is strange however to have 2 war files and place a dependency between them. You will have, to my understanding, 2 web applications. If I guess correctly, Module X is somehow the business layer of the whole system, (includes the services) so is enough to generate a jar file from it, and define only Module Y as a web module. Nevertheless, you need to have an explicit maven dependency to Module X in the pom.xml file of Module Y.

Upvotes: 1

Related Questions