aot.factories 是个啥?| Spring boot 3.0 抢先了解

JAVA架构日记

共 3691字,需浏览 8分钟

 · 2022-10-19

在上篇 Spring 6.0 变更 | Spring boot 3.0 抢先了解 中,我们介绍了 Spring 的一些变更。除了部分依赖包调整,看上去好像变动不大,今天笔者带着大家深入源码来看看其中变化最大——新增的 Graalvm aot 支持。

一、Spring core

Spring framework 6.0 中 Spring core 有了大变更,添加了下列目录:

  1. GraalVM feature —— GraalVM 允许客户端拦截本机映像生成并运行自定义初始化不同阶段的代码。(GraalVM APi 可能随时改变,所以没被纳入到框架公共 API)
  2. aot —— Spring AOT 基础设施的核心包。
  3. javapoet —— 用于生成 Java 源代码的 Java API 包。

javapoet 目录只有一个 package-info.java 文件,编译时会打包到该目录下,内容为 square 组织开源的 javapoet。

/**
 * Spring's repackaging of
 * <a href="https://github.com/square/javapoet">JavaPoet</a>
 * (with Spring-specific utilities; for internal use only).
 */

package org.springframework.javapoet;

GraalVM feature 模块编译之后也会打包到 aot/graalvm 目录。另外 resolrces 目录新增了 aot.factories 文件。

二、Spring beans

添加 Spring bean factories 支持 graalvm AOT。也是添加了 aot 目录和  aot.factories 文件。

二、Spring context

添加应用程序 context  AOT 的支持。

三、其它模块

以上 3 个模块添加了 aot 的扩展接口,Spring framework 其他的模块只需要定义  aot.factories 文件。下面我们看看 aot.factories 文件配置和内容(spring-web\src\main\resources\META-INF\spring\aot.factories):

org.springframework.aot.hint.RuntimeHintsRegistrar= \
org.springframework.http.HttpMimeTypesRuntimeHints,\
org.springframework.http.codec.CodecConfigurerRuntimeHints,\
org.springframework.http.converter.json.JacksonModulesRuntimeHints,\
org.springframework.web.util.WebUtilRuntimeHints

HttpMimeTypesRuntimeHints 中配置 mime.types 资源文件。

class HttpMimeTypesRuntimeHints implements RuntimeHintsRegistrar {

    @Override
    public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
        hints.resources().registerPattern("org/springframework/http/mime.types");
    }
}

CodecConfigurerRuntimeHints 中配置了 CodecConfigurer.properties和其中配置的类。

class CodecConfigurerRuntimeHints implements RuntimeHintsRegistrar {

 @Override
 public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
  hints.resources().registerPattern(
    "org/springframework/http/codec/CodecConfigurer.properties");
  hints.reflection().registerTypes(
    TypeReference.listOf(DefaultClientCodecConfigurer.classDefaultServerCodecConfigurer.class),
    typeHint -> typeHint.onReachableType(CodecConfigurerFactory.class)
      .withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
;
 }

}

CodecConfigurer.properties 文件内容:

# Default CodecConfigurer implementation classes for static Client/ServerCodecConfigurer.create() calls.
# Not meant to be customized by application developers; simply instantiate custom impl classes instead.

org.springframework.http.codec.ClientCodecConfigurer=org.springframework.http.codec.support.DefaultClientCodecConfigurer
org.springframework.http.codec.ServerCodecConfigurer=org.springframework.http.codec.support.DefaultServerCodecConfigurer

四、总结

Spring framework 6.0 添加了 Graalvm aot 支持扩展和  aot.factories 配置文件和规范,可完成对不支持 Graalvm aot 的依赖进行扩展。这样就需要我们在开发 Spring boot starter 时分析和完成对 Graalvm aot 的支持情况。对不支持的,例如:自定义资源文件、反射等使用 aot.factories 文件进行配置。 

在 Spring boot 3.0 之前,我们使用 Graalvm aot 需要引入 spring-native 依赖来扩展对资源文件、反射等支持。详见:Spring Native 可以正式使用了么?

在 Spring boot 3.0 或者 Spring framework 6.0 之后我们可以直接使用 Spring framework 6.0 内置的支持,通过自定义 aot.factories 文件配置来处理。未来 mica-auto 也会支持使用注解来生成 aot.factories 文件。


浏览 41
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报