首页 > 解决方案 > SPI自动注册“com.mysql.cj.jdbc.Driver”的困惑

问题描述

最近我一直在使用 JDBC 开发一个 Java Web 应用程序,我发现它com.mysql.jdbc.Driver自“Connector/J API 8.0”以来已被弃用。

因此,如下所示,Java 运行时警告您使用com.mysql.cj.jdbc.Driver,而不是使用已弃用的驱动程序。

Loading class `com.mysql.jdbc.Driver'. This is deprecated.
The new driver class is `com.mysql.cj.jdbc.Driver'.
The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

我的问题真正在于这条线下。

The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

一开始我以为这意味着注册驱动程序不需要调用Class.forName,但经过测试,我发现我错了。

那么实际上是什么意思manual loading of the driver class is generally unnecessary呢?有什么我错过的吗?

谢谢你。

标签: mysqljakarta-eejdbc

解决方案


MySQL 连接器/J 开发人员指南仍然引用 Class.forName。见https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-usagenotes-connect-drivermanager.html

但从 JDBC 4 开始,使用了服务提供者接口,因此不再需要调用。(对我来说,它不需要 Class.forName 调用!)

SPI的详细信息:


推荐阅读