Java 如何在Maven多模块Spring web应用程序中自动连接类?
我正在开发包含Maven多模块项目的应用程序。当尝试从另一个模块@Autowire一个服务类时,我得到了Java 如何在Maven多模块Spring web应用程序中自动连接类?,java,spring,maven,spring-mvc,Java,Spring,Maven,Spring Mvc,我正在开发包含Maven多模块项目的应用程序。当尝试从另一个模块@Autowire一个服务类时,我得到了java.lang.NoClassDefFoundError:使这个项目唯一的是两个web模块之间的依赖关系 父项目 Pom.xml <project> <groupId>com.test.simple-project</groupId> <artifactId>simple-parent</artifactId>
java.lang.NoClassDefFoundError:
使这个项目唯一的是两个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>
<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>
<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
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>
<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>
<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>
两个模块都是SpringMVCweb项目(pom.xml打包都是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)
有什么想法吗?您必须在与模块Y关联的pom.xml文件中定义对模块X的输出war文件的依赖关系。 然而,有两个war文件并在它们之间放置依赖项是很奇怪的。据我所知,您将拥有2个web应用程序。如果我猜对了,模块X不知何故是整个系统的业务层(包括服务),因此足以从中生成jar文件,并将模块Y定义为web模块。
但是,您需要在模块Y的pom.xml文件中明确地对模块X具有maven依赖关系。依赖于另一个WAR模块的WAR模块无法访问依赖模块生成的类。因此,您必须将
module-x
中的类提供给module-y
中的类。有两种方法可以做到这一点
备选方案1:更清洁的设计
将公共类抽象到一个单独的JAR模块中,并声明从
module-x
和module-y
到该模块的依赖关系
选项2:从module-y
首先,在
module-x
中配置maven-war插件
,将这些类打包为一个单独的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>
很难说,除非你包含更多的信息——最好是一些pom.xml——war插件的更多信息都包含在选项2中,但是我得到了同样的错误。你认为这与组件扫描有关吗?如果是相同的堆栈跟踪,那么组件扫描与此无关。如果这是一个组件扫描错误,就会出现类似于
没有匹配类型的bean的情况。。。找到
。这是一个工作样本。将其与代码进行比较,找出导致错误的差异。如果这不起作用,请发布一个完整的示例项目来演示您的问题。感谢您花时间创建示例项目。我正在检查它,会让你知道结果的。刚刚开始在这个项目上再次工作。您提供的示例项目工作正常,但我更改了pom.xml以支持在eclipse中定义的服务器上运行应用程序,并且我得到了org.springframework.beans.factory.BeanCreationException:创建名为“fooController”的bean,该名称在文件…\…\fooController.class]中定义:bean类型的后期处理失败[class org.example.web.FooController]失败;嵌套异常为java.lang.IllegalStateException:未能为资源元数据内省bean类[org.example.web.FooController]:找不到它所依赖的类
<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>