簡體   English   中英

依賴注入-Maven多模塊項目上的java.lang.NoClassDefFoundError

[英]Dependency injection - java.lang.NoClassDefFoundError on a maven multi module project

我從事一個Maven多模塊項目。 這些模塊是:持久性模塊(稱為打包為jar entities ,服務模塊(稱為打包為jar services和Web模塊(打包為war 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。

實體-的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>

服務-的context.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>

網絡/ 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模塊中的服務應用程序上下文文件?

您專門從Web POM中排除了jar:

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

這是您在運行時需要的可傳遞依賴項。

您會收到一個NoClassDefFoundError,因為您可以在此處編譯代碼,但在運行時需要它。

只需刪除排除項,實體jar將在運行時添加。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM