如何在Maven多模块Spring Web应用程序中自动挂载类?
我正在开发包含Maven多模块项目的应用程序。 当从另一个模块尝试@Autowire服务类时,我得到java.lang.NoClassDefFoundError:
这个项目的独特之处在于两个Web模块之间的依赖关系。
家长项目
的pom.xml
com.test.simple-project simple-parent pom module-x module-y org.apache.maven.plugins maven-compiler-plugin 2.3.2 1.5 1.5
模块X.
的pom.xml
module-x ${project.groupId} simple-parent war ... maven-eclipse-plugin 2.9 org.springframework.ide.eclipse.core.springnature org.springframework.ide.eclipse.core.springbuilder true true org.apache.maven.plugins maven-compiler-plugin 2.5.1 1.6 1.6 -Xlint:all true true org.codehaus.mojo exec-maven-plugin 1.2.1 org.test.int1.Main
package com.test.module-x.service; @Service("userService") public class UserServiceImpl implements UserService { }
模块Y.
的pom.xml
module-y ${project.groupId} simple-parent ${project.version} war com.test.simpleproject module-x ... maven-eclipse-plugin 2.9 org.springframework.ide.eclipse.core.springnature org.springframework.ide.eclipse.core.springbuilder true true org.apache.maven.plugins maven-compiler-plugin 2.5.1 1.6 1.6 -Xlint:all true true org.codehaus.mojo exec-maven-plugin 1.2.1 org.test.int1.Main
Y中的分量扫描
@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-y
类访问module-x
声明的类
首先,在module-x
配置maven-war-plugin
,将类打包为一个单独的JAR文件。
module-x war org.apache.maven.plugins maven-war-plugin 2.4 true classes
现在,在module-y
声明对module-x
的依赖:
module-y war ${project.groupId} module-x ${project.version} classes
您必须在与模块Y关联的pom.xml文件中定义对模块X的输出war文件的依赖。然而,有两个war文件并在它们之间放置依赖关系是奇怪的。 根据我的理解,您将拥有2个Web应用程序。 如果我猜对了,模块X在某种程度上是整个系统的业务层,(包括服务),因此足以从中生成jar文件,并仅将模块Y定义为Web模块。 但是,您需要在模块Y的pom.xml文件中对模块X具有显式的maven依赖关系。