前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot 3.0 抢先了解:aot.factories 是个啥?

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

作者头像
程序猿DD
发布2023-04-04 11:19:00
1.2K0
发布2023-04-04 11:19:00
举报
文章被收录于专栏:程序猿DD

今天带大家深入源码来看看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 包。

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

代码语言:javascript
复制
/**
 * 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):

代码语言:javascript
复制
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 资源文件。

代码语言:javascript
复制
class HttpMimeTypesRuntimeHints implements RuntimeHintsRegistrar {

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

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

代码语言:javascript
复制
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 文件内容:

代码语言:javascript
复制
# 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学习资料

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-10-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序猿DD 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Spring core
  • 二、Spring beans
  • 二、Spring context
  • 三、其它模块
  • 四、总结
    • 推荐阅读
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档