Spring Boot 3.0 抢先了解:aot.factories 是个啥?

今天带大家深入源码来看看Spring Boot 3.0 中的一个大变化:新增的 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 包。

03941660da7cb49d41081203da77f307.png

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 目录。

2810d0d9560c836d0a314b7358ac9987.png

另外 resolrces 目录新增了 aot.factories 文件。

7c4c6238c23cac32aa2191dffc120a7d.png

二、Spring beans

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

05e5b00a81e3b606589c5cecd2088a48.png

二、Spring context

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

819778a21fbee12a4316e1f6cbb25474.png

三、其它模块

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

------

我们创建了一个高质量的技术交流群,与优秀的人在一起,自己也会优秀起来,赶紧点击加群,享受一起成长的快乐。另外,如果你最近想跳槽的话,年前我花了2周时间收集了一波大厂面经,节后准备跳槽的可以点击这里领取

推荐阅读

··································

你好,我是程序猿DD,10年开发老司机、阿里云MVP、腾讯云TVP、出过书创过业、国企4年互联网6年。从普通开发到架构师、再到合伙人。一路过来,给我最深的感受就是一定要不断学习并关注前沿。只要你能坚持下来,多思考、少抱怨、勤动手,就很容易实现弯道超车!所以,不要问我现在干什么是否来得及。如果你看好一个事情,一定是坚持了才能看到希望,而不是看到希望才去坚持。相信我,只要坚持下来,你一定比现在更好!如果你还没什么方向,可以先关注我,这里会经常分享一些前沿资讯,帮你积累弯道超车的资本。

点击领取2022最新10000T学习资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值