【问题标题】:Dependency injection - java.lang.NoClassDefFoundError on a maven multi module project依赖注入 - Maven 多模块项目上的 java.lang.NoClassDefFoundError
【发布时间】: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 模块中的服务应用程序上下文文件的方式是否正确?

【问题讨论】:

    标签: java spring maven


    【解决方案1】:

    您专门从网络 POM 中排除了 jar:

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

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

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

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

    【讨论】:

    • 但是 web 模块应该只“知道”和“需要”服务模块。我正在为此目的创建 DTO。为了能够排除实体。
    • 在您的情况下,这是一个需要的 DAO,而不是 DTO。但在这两种情况下,我相信你的 DAO 接口在方法的签名中也有 DTO 类。你可以确保某种松散耦合,但如果你想使用一个类,你必须提供它
    • 您可以做的是将 dto 放在另一个项目中,在您的服务层/项目中,您可以将实体类映射到 dto 类。不过,我不确定这是否值得。
    • 那里的导入怎么样:` ` ? Maibe 是不正确的导入。
    • 我删除了排除项。我现在得到这个错误:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.iquest.af.dao.StudentDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2011-10-14
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多