如何在Maven多模块Spring Web应用程序中自动挂载类? [英] How to autowire class in Maven multi module Spring web app?

查看:231
本文介绍了如何在Maven多模块Spring Web应用程序中自动挂载类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发包含Maven多模块项目的应用程序。当从另一个模块尝试@Autowire服务类时,我得到 java.lang.NoClassDefFoundError:这个项目的独特之处在于两个Web模块之间的依赖关系。

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.

父项目

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


component-scan in Y

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


 @Controller
     public class SimpleController {

        @Autowired
        private UserService userService;
    }

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

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

运行应用程序抛出 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 中的类。有两种方法可以做到。

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.

将公共类抽象到一个单独的JAR模块中,并从 module-x module-y 到这个模块。

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

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

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>

现在,宣布依赖于 module-x in module -y

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>

这篇关于如何在Maven多模块Spring Web应用程序中自动挂载类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆