java-如何在Maven多模块Spring Web应用程序中自动装配类?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何在Maven多模块Spring Web应用程序中自动装配类?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7023字,纯文字阅读大概需要11分钟。
内容图文
我正在开发包含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模块无法访问由依赖关系模块生成的类.因此,您将必须将模块x中的类提供给模块y中的类.有两种方法可以做到这一点.
选项1:更清洁的设计
将公共类抽象到单独的JAR模块中,并声明从module-x和module-y到此模块的依赖关系.
选项2:从模块y中的类访问在模块x中声明的类
首先,在module-x中配置maven-war-plugin以将类打包为单独的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-y中声明对module-x的依赖关系:
<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>
内容总结
以上是互联网集市为您收集整理的java-如何在Maven多模块Spring Web应用程序中自动装配类?全部内容,希望文章能够帮你解决java-如何在Maven多模块Spring Web应用程序中自动装配类?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。