我正在try 使用JPMS和Gradle多项目功能创建一个多模块项目.我有3个测试模块:应用程序、教师和学生.学生依赖于老师,应用程序依赖于两者.学生和教师模块有自己的控制器、服务、实体和存储库.每个模块都有模块信息.java文件.

module app {
    requires java.sql;
    requires org.hibernate.orm.core;

    requires spring.boot;
    requires spring.data.jpa;
    requires spring.boot.autoconfigure;

    requires spring.core;
    requires spring.beans;
    requires spring.context;

    requires teacher;
    requires student;

    opens az.chaypay.app to spring.core;
    exports az.chaypay.app to spring.beans, spring.context;
}

module teacher {
    requires static lombok;

    requires spring.web;

    exports az.chaypay.teacher to app;
    exports az.chaypay.teacher.api to student;
    exports az.chaypay.teacher.controller to spring.beans;
    exports az.chaypay.teacher.service.impl to spring.beans;

    opens az.chaypay.teacher.controller to spring.core;
    opens az.chaypay.teacher.repository.dao to spring.core, com.fasterxml.jackson.databind, org.hibernate.orm.core;
}

module student {
    requires static lombok;
    requires com.fasterxml.jackson.databind;

    requires spring.web;

    exports az.chaypay.student to app;
    exports az.chaypay.student.controller to spring.beans;
    exports az.chaypay.student.service.impl to spring.beans;

    opens az.chaypay.student.controller to spring.core;
    opens az.chaypay.student.repository.dao to spring.core, com.fasterxml.jackson.databind, org.hibernate.orm.core;

    requires teacher;
}

当我使用这些设置运行应用程序时,一切都正常.应用程序已成功构建并运行.但IntelliJ抱怨进口.

enter image description here

包裹组织.springframework.数据jpa.存储库"在模块"spring中声明.数据jpa,但模块"学生"不读

我不明白,如果Java在导入方面有问题,那么为什么我的应用程序运行时没有错误.为什么我的应用程序运行时没有requires语句?

推荐答案

它"似乎"起作用的原因是spring-data-jpa不是一个模块依赖项.可以使用自动模块名spring.data.jpa将其包含在模块化项目中.

requires spring.data.jpa添加到app模块时,它可以在未命名模块的modulepath(及其所有包都已导出)上使用.

但一旦到了那里,当你执行它时,student实际上可以看到同一个未命名的模块,所以它"碰巧"工作,但它是不正确的.

appstudent模块都依赖于org.springframework.data.jpa.repository包中的类,因此IntelliJ建议在student模块描述符中添加requires spring.data.jpa是正确的.

Java相关问答推荐

如何从片段请求数据到活动?在主要活动中单击按钮请求数据?

我应该避免在Android中创建类并在运行时编译它们吗?

Java中不同包中的类之间的配置共享

是否保证在事务性块的末尾标记违反约束?

Spring Boot@Cachebale批注未按预期工作

名称冲突具有相同的擦除

如何创建同一类的另一个对象,该对象位于变量中?

内存中的H2修剪尾随空格

相同的Java SerializedLambda为implMethodKind返回不同的结果

Java:使用Class.cast()将对象转换为原始数组

如何从错误通道回复网关,使其不会挂起

Spring Data JPA慢慢地创建了太多非活动会话

PDFBox未加载内容

Javadoc在方法摘要中省略方法

无法使用Freemarker从XML中读取重复的标记值

AWS Java SDK v2.x中没有setObjectAcl方法

当我在Java中有一个Synchronized块来递增int时,必须声明一个变量Volatile吗?

有没有办法知道在合并中执行了什么操作?

具有 DayOfWeek 列表的 JPA 实体

更新不可变的深层嵌套字段