为什么 javac 抱怨命名的自动模块?

Why does javac complain about named automatic-modules?

如果 module-info.java 引用设置了 "Automatic-Module-Name" 的自动模块,为什么 Java 9 编译器会警告 "requires directive for an automatic module"?这样的模块有什么风险?

这个问题与 不完全相同,因为后者没有解决我所引用的编译器警告背后的具体原因(问题的上下文很重要)。也就是说,它对后续阅读很有用 link。

引用 Remi Forax:

The main issue is that an automatic module can see classes from the classpath, but it also exports all its package so there is no encapsulation, and once you require one automatic module all automatic modules from the module path are visible.

So an automatic module is a great tool when you transitioned to the module world, but in fine, you do not want any automatic modules in you dependency graph.

好吧,问题是自动模块需要传递所有其他模块。因此,通过要求它,您会用很多东西污染依赖关系图,之后您可能会得到非常令人惊讶的行为。

想象一下: 您有一个自动模块 com.logging,您的(显式)模块 my.app 需要它。但是,您还有另一个名为 com.dbconnectors 的自动模块(my.app 不需要,只是驻留在模块路径中)。它将被加载,因为当需要一个自动模块时,所有其他模块也将被解析。 my.app 也可以访问它,因为 com.logging 需要它传递。

然后,当您的模块 my.app 需要 com.logging 时,您使用 com.dbconnectors "magically" 中的内容的代码可以工作,而当您删除 requires com.logging 来自模块描述符。

一般来说,应该谨慎使用传递依赖。