英文:
How does SpringBoot 2.7.x support multi-version JDK
问题
SpringBoot 2.7.x已知是使用JDK 1.8开发的,但当引入Spring Boot 2.7.x依赖后,我们可以在JDK 1.8、JDK 11和JDK 17上进行开发和运行。
我想了解Spring Boot如何支持多版本的JDK运行,以及SpringBoot如何处理废弃的API或依赖(例如,如果我们使用JDK 11,rt.jar
将不会包含在类路径中,SpringBoot如何解决这个问题)。SpringBoot是避免使用废弃API还是根据JDK版本选择API的方式?
在我看来,如果SpringBoot 2.7.x使用在JDK 8+中移除的API,那么使用JDK 11或JDK 17的用户在启动应用程序时将会遇到ClassNotFoundException
。有人能告诉我SpringBoot是如何解决这个问题的吗?还是它们只是使用可以在JDK 1.8、JDK 11和JDK 17中使用的API。
提前感谢!
英文:
SpringBoot 2.7.x is known to be developed using JDK 1.8, but when we introduce the Spring Boot 2.7.x dependency, we can develop and run on JDK 1.8, JDK 11, and JDK 17.
I want to figure out how Spring Boot support multi-version JDK running, and how SpringBoot handle deprecated API or dependency(e.g if we use JDK 11, rt.jar
will not inclued in classpath, how SpringBoot reslove this problem?). is SpringBoot avoid to use deprecated API or is there a way to choose an API based on the JDK version.
In my opinion, if SpringBoot 2.7.x use API which is removed in JDK 8+, users who use JDK 11 or JDK 17 will encounter ClassNotFoundException
when start the application. Could anyone tell me how SpingBoot resolve this problem? Or are they just using APIs that can be used in JDK 1.8, JDK 11, and JDK 17.
Thanks in advance!
答案1
得分: 2
通常情况下,Java 非常向后兼容,很少移除API。这意味着在Java 8中存在但在Java 17中不存在的API非常少。
或者他们只是使用可以在JDK 1.8、JDK 11和JDK 17中使用的API。
由于上述向后兼容性的描述,目前Spring Boot能够无实际劣势地执行以下操作。
这将在Java 21中有所改变。Spring Boot 3.2将添加对虚拟线程的支持,虚拟线程是Java 21中新引入的(除了早期的预览版本之外)。Spring Framework的虚拟线程支持是通过使用多版本jar实现的,其中包含与Java 21特定代码互操作的代码。这将允许Spring Framework 6.1和Spring Boot 3.2在继续支持早期版本的JDK的同时支持虚拟线程。Spring Boot将使用其@ConditionalOnJava
支持在Java 21及更高版本上启用Spring Framework的虚拟线程支持。
英文:
Generally speaking, Java is very backwards compatible and APIs are rarely removed. This means that there are very few APIs that exist in Java 8 but don't exist in Java 17.
> Or are they just using APIs that can be used in JDK 1.8, JDK 11, and JDK 17.
Thanks to the backwards compatibility described above, this is what Spring Boot is able to do at the moment with no real disadvantages.
This will change a little with Java 21. Spring Boot 3.2 will add support for virtual threads which are new, (other than earlier preview releases) in Java 21. Spring Framework's virtual thread support is implemented using a multi-release jar with Java 21-specific code interacting with the JDK's virtual threads APIs. This will allow Spring Framework 6.1 and Spring Boot 3.2 to support virtual threads while also continuing to support earlier JDKs. Spring Boot will enable Spring Framework's virtual thread support on Java 21 and later using its @ConditionalOnJava
support.
答案2
得分: 1
SpringBoot 2.7.x 如何支持多版本 JDK?
这被称为 工具链支持。它允许您为 Spring Boot 应用程序的不同构建指定不同的 JDK 版本,这是 Apache Maven 中的一个几乎不是新功能,它允许您指定不同版本的 JDK。
要做到这一点,您可以在项目的根目录中简单地添加 toolchains.xml
文件,其中指定您希望用于构建和运行应用程序的 JDK 版本。
<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>11</version>
</provides>
<configuration>
<jdkHome>/pathtojdk11/jdk11</jdkHome>
...
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>17</version>
</provides>
<configuration>
<jdkHome>/pathtojdk17/jdk17</jdkHome>
...
</configuration>
</toolchain>
</toolchains>
Gradle 也支持这个功能,参见 这里。
有人能告诉我 SpringBoot 如何解决
ClassNotFoundException
问题吗?
Spring 不会解决这个问题,实际上这与框架无关,它在 运行时
发生,当 JVM 无法找到在您的代码中引用的类时发生(与 JDK 版本更改类似),有许多策略可以防止这种情况发生,大多数与构建工具及其插件相关。
英文:
> How does SpringBoot 2.7.x support multi-version JDK ?
It's called Toolchains support. Which allows you to specify a different JDK version for different builds for Spring Boot application,
It (almost not new) feature in Apache Maven that allows you to specify a different version of the JDK(s).
To do so you can simply add toolchains.xml
file in your project's root directory which in that specifies the JDK version that you wish to use for building and running your application.
<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
<toolchain>
<type>jdk</type>
<provides>
<version>11</version>
</provides>
<configuration>
<jdkHome>/pathtojdk11/jdk11</jdkHome>
...
</configuration>
</toolchain>
<toolchain>
<type>jdk</type>
<provides>
<version>17</version>
</provides>
<configuration>
<jdkHome>/pathtojdk17/jdk17</jdkHome>
...
</configuration>
</toolchain>
</toolchains>
<!-- end snippet -->
Gradle also does, see here.
> Could anyone tell me how SpingBoot resolve ClassNotFoundException
problem?
Spring does not,It's not related to framework actually it occur during runtime
when the JVM is unable to find a class that is referenced in your code (same like JDK version changes) and they are many strategies to prevent it most of them are related to build tools and their plugins.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论