依赖注入 - maven多模块项目上的java.lang.NoClassDefFoundError

时间:2015-02-26 12:10:39

标签: java spring maven

我在maven多模块项目上工作。这些模块是:名为entities的持久性模块打包为jar,名为services的服务模块打包为jar,名为web的Web模块打包为{ {1}}。 pom如下:

war

每个模块都有自己的应用程序上下文文件,我在其中声明bean。

实体-context.xml中

<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>

服务-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 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

Web模块配置文件

<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>

网/ pom.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" 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";
    }


 }

谁能告诉我我做错了什么?

编辑:我访问服务模块中实体jar的应用程序上下文文件,然后访问Web模块中的服务应用程序上下文文件是正确的吗?

1 个答案:

答案 0 :(得分:0)

您明确地从网络POM中排除了jar:

<exclusions>
            <exclusion>
                <groupId>com.af</groupId>
                <artifactId>entities</artifactId>
            </exclusion>
        </exclusions>

这是运行时需要的传递依赖。

你得到一个NoClassDefFoundError,因为你能够编译代码,但在运行时你需要它。

只需删除排除项,就会在运行时添加实体jar。

相关问题