Java 类文件在 Jar 包中多版本共存

Java Maven About 1,787 words

目标

Extend the JAR file format to allow multiple, Java-release-specific versions of class files to coexist in a single archive.

允许Jar包存在多版本的字节码文件。

Spring Boot 源码

Spring Boot在处理虚拟线程时,主包中使用了VirtualThreadDelegate类,在versions/21包下定义了在JDK21中的具体实现。

Maven/Gradle等打包工具会将相应的打包打包到对应的JDK目录下(versions/<JDK version>)。

主包代码

package org.springframework.core.task;

final class VirtualThreadDelegate {

    public VirtualThreadDelegate() {
        throw new UnsupportedOperationException("Virtual threads not supported on JDK <21");
    }

    public ThreadFactory virtualThreadFactory() {
        throw new UnsupportedOperationException();
    }

    public ThreadFactory virtualThreadFactory(String threadNamePrefix) {
        throw new UnsupportedOperationException();
    }

    public Thread newVirtualThread(String name, Runnable task) {
        throw new UnsupportedOperationException();
    }

}

versions/21 包下代码

package org.springframework.core.task;

import java.util.concurrent.ThreadFactory;

final class VirtualThreadDelegate {
    private final Thread.Builder threadBuilder = Thread.ofVirtual();

    public ThreadFactory virtualThreadFactory() {
        return this.threadBuilder.factory();
    }

    public ThreadFactory virtualThreadFactory(String threadNamePrefix) {
        return this.threadBuilder.name(threadNamePrefix, 0L).factory();
    }

    public Thread newVirtualThread(String name, Runnable task) {
        return this.threadBuilder.name(name).unstarted(task);
    }
}

MANIFEST.MF

打包工具会指定Multi-Releasetrue

Manifest-Version: 1.0
Implementation-Title: spring-core
Implementation-Version: 6.1.6
Automatic-Module-Name: spring.core
Created-By: 17.0.10 (Oracle Corporation)
Multi-Release: true
Dependencies: jdk.unsupported,org.jboss.vfs

JEP 238

https://openjdk.org/jeps/238

Views: 29 · Posted: 2026-01-04

———         Thanks for Reading         ———

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓
Prev Post
Next Post
Today In History
Browsing Refresh