Java 规范实现 - 从那里获取正在实现的接口的导入

Java specification implementation - from where it gets the imports of the interfaces being implemented

我很难理解各种供应商如何实现规范,例如 JPA 或 JDBC 等

基本上的疑问是,供应商要实现规范,就需要实现接口(尽管这可能是简化的陈述)。

以 msql jdbc 驱动程序为例,例如它在 class ConnectionImpl:

中导入 import java.sql.Connection

现在我的疑问是,java.sql.Connection从哪里来?

规范是如何打包的?它们是否像接口一样放置,classes 和包作为 .jar?

我知道 java.sql.Connection 是 rt.jar 的一部分,那么这是否意味着 JDBC 驱动程序应该有这个 rt.jar?如果是这样,那么它是否意味着 rt.jar 也作为 jdbc 驱动程序的一部分出现(否则它将如何编译?)。

我对此感到困惑,谁能帮助我更好地理解它?

java.sql.Connection其实只是一个接口。但这不是这里的问题。

JDBC 的情况是 java.sql.Driver 提供了不同的组件; JDBC 驱动程序需要使用服务提供商来实现 class;有关详细信息,请参阅 the javadoc of DriverManager

每个主要 Sun/Oracle API(或 JSR)像 JDBC 都有对应的 TCKTechnology Compatibility Kit),测试实现API 合规性。

如果你想实现你应该 pass tests from the community process,但是 API 没有指定 jar 放置 java.sql.Connection

一般来说,甚至不可能指定 jar - jar 列表不是 Java 运行时规范的一部分,并且是 vendor/platform/jvm 特定的(例如 Oracle JVM9 没有 rt.jar AT ALL anymore):

The class and resource files previously stored in lib/rt.jar, lib/tools.jar, lib/dt.jar, and various other internal jar files will now be stored in a more efficient format in implementation-specific files in the lib directory. The format of these files will not be specified and is subject to change without notice.

所以你只需要确保所有必要的接口和类都在类路径中

I know java.sql.Connection is part of rt.jar, so does it mean that JDBC driver should have this rt.jar?

不,没有。供应商假定 java.sql.Connection 或任何其他标准 Java 平台 类 由您的运行时提供(已通过认证并且也是 TCK 进程的主题)并且类加载器将在类路径中找到它们。

任何人都可以编写名为 java.sql.Connection 的包的实现。甚至你。供应商可能会编写自己的接口。java 接口文件。

唯一不那么直接的包是 java.lang,因为那里的包可以与 JVM 耦合。