首页 > 解决方案 > 拼图“需要传递”不起作用

问题描述

您好我目前遇到的问题是“需要传递”指令没有在我的依赖模块中打开使用的模块。

为了熟悉 jigsaw,我开始使用 spring 和 JDK 10 编写一个新应用程序。我有一个名为“ database ”的模块,它使用spring.data.jpa模块。此外,我还有一个名为“ mvc ”的模块,它需要模块“ database ”。

现在在模块“数据库”中的 module-info.java 中,我定义了 spring 模块,如下所示:

requires transitive spring.data.jpa;

我希望这个模块也可以在我的模块“ mvc ”中使用,但我没有。有什么建议我做错了吗?

模块数据库的module-info.java

 module database {

    requires java.sql;
    requires java.persistence;
    requires liquibase.core;

    requires spring.beans;
    requires transitive spring.data.jpa;
    requires spring.jdbc;
    requires spring.tx;
    requires spring.orm;

    exports de.database.entities to mvc;
    exports de.database.repositories to mvc;
  }

模块mvc的 module-info.java

module mvc {

    requires database;

    requires spring.context;
    requires spring.beans;
    requires spring.boot;

    exports de.mvc to application;

}

正如我所说 spring.data.jpa 无法在模块mvc中访问。此外,require 指令要求 spring.data.jpa是不可能的。

编辑:

数据库-> build.gradle

dependencies {
    implementation(group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa')

    implementation group: 'org.hsqldb', name: 'hsqldb'
    implementation group: 'org.hibernate.javax.persistence', name: 'hibernate-jpa-2.1-api'
    implementation(group: 'org.liquibase', name: 'liquibase-core')

    implementation group: 'javax.xml.bind', name: 'jaxb-api'

    testImplementation (group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2')
}

mvc -> build.gradle

dependencies {
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2'
    implementation (group: 'org.springframework.boot', name: 'spring-boot-starter') {
        force = true
        exclude group: 'ch.qos.logback', module: 'logback-classic'
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }

//    implementation group: 'org.springframework.data', name: 'spring-data-jpa'

    implementation project(":database")
}

这里的问题是,如果我 在模块mvc中未注释依赖spring-data-jpa ,则编译将失败。我需要在 build.gradle 中显式启用它,即使它应该作为数据库模块的传递依赖项存在。

我的错误是

> Task :mvc:compileJava FAILED
error: module not found: spring.data.jpa
1 error

我尝试将数据库部分模块化。数据库部分使用 spring-repositories。所以我使用模块 spring.data.jpa 中的接口JpaRepository,如下所示:

@Repository
public interface DestinationDao
  extends JpaRepository<Destination, Long>, JpaSpecificationExecutor<Destination>
{

}

现在我想在我的模块mvc中使用这个 bean 。即使我没有在模块mvc中明确定义对 spring-data-jpa 的依赖,这在我看来也应该有效。

标签: javajava-9java-modulejava-platform-module-systemrequires

解决方案


您的模块声明是正确的,mvc应该是spring.data.jpa

我猜你得到一个编译错误?我不是 Gradle 专家,所以我在这里可能错了,但我认为 Gradle 有一种模式,它只在编译期间使用直接依赖项。如果spring.data.jpa不在mvc的依赖列表中(在 Gradle 中),那么这将解释问题。

要弄清楚这一点,请运行 Gradle--debug并查找调试消息“编译器参数:”。

如果不是这种情况,请准确分享您何时以及遇到什么错误。


推荐阅读