依赖项注入-maven多模块项目上的java.lang.NoClassDefFoundError
我从事一个maven多模块项目。这些模块是:称为依赖项注入-maven多模块项目上的java.lang.NoClassDefFoundError,java,spring,maven,Java,Spring,Maven,我从事一个maven多模块项目。这些模块是:称为实体的持久化模块打包为jar,称为服务的服务模块打包为jar,称为web打包为war。 pom如下所示: <groupId>com.af</groupId> <artifactId>eMuse</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <modu
实体的持久化模块
打包为jar
,称为服务的服务模块
打包为jar
,称为web
打包为war
。
pom如下所示:
<groupId>com.af</groupId>
<artifactId>eMuse</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>entities</module>
<module>services</module>
<module>web</module>
</modules>
和web模块配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.af" />
<import resource="classpath*:services-context.xml"/>
<!-- Tiles configuration -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles2.TilesView</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<mvc:default-servlet-handler />
</beans>
我得到一个例外:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.af.service.StudentService com.af.controller.TestController.studentService; nested exception is java.lang.NoClassDefFoundError: Lcom/af/service/StudentService;
谁能告诉我我做错了什么
编辑:我访问服务模块中实体jar的应用程序上下文文件,然后访问web模块中的服务应用程序上下文文件的方式是正确的?您明确地从web POM中排除了jar:
<exclusions>
<exclusion>
<groupId>com.af</groupId>
<artifactId>entities</artifactId>
</exclusion>
</exclusions>
com.af
实体
这是运行时需要的可传递依赖项
您会得到一个NoClassDefFoundError,因为您可以在哪里编译代码,但在运行时需要它
只需删除排除项,实体jar将在运行时添加。但是web模块应该只“知道”和“需要”服务模块。我创建DTO就是为了这个目的。在您的例子中,这是一个需要的DAO,而不是DTO。但在这两种情况下,我相信您的DAO接口在方法的签名中也有DTO类。您可以确保某种松散耦合,但是如果您想要使用一个类,您必须提供它,您可以做的是将dto放在另一个项目中,并且在您的服务层/项目中,您可以将实体类映射到dto类。但我不确定这是否值得付出努力。那进口商品呢:```?Maibe是一个不正确的导入。我删除了排除项。现在我得到了这个错误:
org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到依赖项类型为[com.iquest.af.dao.StudentDAO]的符合条件的bean:至少需要1个符合此依赖项autowire候选项条件的bean。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.af" />
<import resource="classpath*:services-context.xml"/>
<!-- Tiles configuration -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.tiles2.TilesView</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<mvc:default-servlet-handler />
</beans>
<parent>
<groupId>com.af</groupId>
<artifactId>eMuse</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>web</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.af</groupId>
<artifactId>services</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>com.af</groupId>
<artifactId>entities</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- other dependencies-->
</dependencies>
@Controller
public class TestController {
@Autowired
private StudentService studentService;
@RequestMapping(value="/index", method = RequestMethod.GET)
public String test(Model model){
StudentModel stud = StudentModelMapper.mapStudentDTO(studentService.getStudentById(1));
model.addAttribute("name", stud.getFirstName());
return "index";
}
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.af.service.StudentService com.af.controller.TestController.studentService; nested exception is java.lang.NoClassDefFoundError: Lcom/af/service/StudentService;
<exclusions>
<exclusion>
<groupId>com.af</groupId>
<artifactId>entities</artifactId>
</exclusion>
</exclusions>