【发布时间】:2015-02-26 12:10:39
【问题描述】:
我从事一个 Maven 多模块项目。这些模块是:名为entities 的持久性模块被打包为jar,名为services 的服务模块被打包为jar 和名为web 的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>
每个模块都有自己的应用程序上下文文件,我在其中声明 bean。
entities-context.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";
}
}
我得到了例外:
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 模块中的服务应用程序上下文文件的方式是否正确?
【问题讨论】: