我从事一个 Maven 多模块项目。这些模块是:打包为 jar
的名为 entities
的持久性模块、打包为 jar
名为 services
的服务模块以及打包为 war
名为 web
的 Web 模块。
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>
每个模块都有自己的应用程序上下文文件,我在其中声明 bean。
实体上下文.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="actionDAO" class="com.af.dao.impl.ActionDAOImpl" />
<bean id="milestoneDAO" class="com.af.dao.impl.MilestoneDAOImpl" />
<bean id="milestoneMarksDAO" class="com.af.dao.impl.MilestoneMarksDAOImpl" />
<bean id="milestoneTypeDAO" class="com.af.dao.impl.MilestoneTypeDAOImpl" />
<bean id="studentDAO" class="com.af.dao.impl.StudentDAOImpl" />
<bean id="studentsActionsDAO" class="com.af.dao.impl.StudentsActionsDAOImpl"></bean>
<bean id="teamDAO" class="com.af.dao.impl.TeamDAOImpl" />
<bean id="toolDAO" class="com.af.dao.impl.ToolDAOImpl" />
</beans>
服务上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentService" class="com.af.service.impl.StudentServiceImpl"/>
<property name="studentDAO" ref="studentDAO"/>
<property name="teamDAO" ref="teamDAO"/>
</bean>
<import resource="/entities-context.xml"/>
</beans>
还在服务 pom.xml
中,我添加了对实体模块的依赖项。
<parent>
<groupId>com.af</groupId>
<artifactId>eMuse</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>services</artifactId>
<dependency>
<groupId>com.af</groupId>
<artifactId>entities</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
以及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>
web/pom.xml
<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";
}
}
我得到了异常(exception):
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>
这是运行时所需的传递依赖。
你会得到一个NoClassDefFoundError,因为你可以编译代码,但在运行时你需要它。
只需删除排除项,实体 jar 就会在运行时添加。
关于java - 依赖注入(inject) - maven 多模块项目上的 java.lang.NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28742121/