如何在Maven多模块Spring Web应用程序中自动装配类?


问题内容

我正在开发包含Maven多模块项目的应用程序。当尝试@Autowire来自另一个模块的服务类时,我得到了java.lang.NoClassDefFoundError:让该项目与众不同的是,依赖关系在2个Web模块之间。

父项目


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>

模块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 {
}

模块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>

Y分量扫描

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






 @Controller
     public class SimpleController {

        @Autowired
        private UserService userService;
    }

这两个模块都是Spring mvc Web 项目(两个pom.xml打包都是战争)

运行应用抛出 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)

有任何想法吗?


问题答案:

与另一个WAR模块具有依赖关系的WAR模块无法访问由依赖关系模块生成的类。因此,你必须提供在类module-x中的那些module-y。有两种方法可以做到这一点。

选项1:更清洁的设计


将公共类抽象到一个单独的JAR模块中,并声明二者之间的依赖关系module-x以及module-y对此模块的依赖。

选项2:可访问从中的类声明的module-xmodule-y


首先,将maven-war-pluginin 配置为module-x将类打包为单独的JAR文件。

<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>

现在,声明对module-x中的依赖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>