您的位置:首页 >聚焦 >

全球热消息:aot.factories 是个啥?| Spring boot 3.0 抢先了解

2022-10-19 16:13:13    来源:程序员客栈

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

一、Spring core

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


(资料图)

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

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

/***Spring"srepackagingof*JavaPoet*(withSpring-specificutilities;forinternaluseonly).*/packageorg.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资源文件。

classHttpMimeTypesRuntimeHintsimplementsRuntimeHintsRegistrar{@OverridepublicvoidregisterHints(RuntimeHintshints,@NullableClassLoaderclassLoader){hints.resources().registerPattern("org/springframework/http/mime.types");}}

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

classCodecConfigurerRuntimeHintsimplementsRuntimeHintsRegistrar{@OverridepublicvoidregisterHints(RuntimeHintshints,@NullableClassLoaderclassLoader){hints.resources().registerPattern("org/springframework/http/codec/CodecConfigurer.properties");hints.reflection().registerTypes(TypeReference.listOf(DefaultClientCodecConfigurer.class,DefaultServerCodecConfigurer.class),typeHint->typeHint.onReachableType(CodecConfigurerFactory.class).withMembers(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS));}}

CodecConfigurer.properties文件内容:

#DefaultCodecConfigurerimplementationclassesforstaticClient/ServerCodecConfigurer.create()calls.#Notmeanttobecustomizedbyapplicationdevelopers;simplyinstantiatecustomimplclassesinstead.org.springframework.http.codec.ClientCodecConfigurer=org.springframework.http.codec.support.DefaultClientCodecConfigurerorg.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文件。

关键词: 文件配置 下面我们 扩展接口

相关阅读