全球热消息:aot.factories 是个啥?| Spring boot 3.0 抢先了解
在上篇Spring 6.0 变更 | Spring boot 3.0 抢先了解 中,我们介绍了 Spring 的一些变更。除了部分依赖包调整,看上去好像变动不大,今天笔者带着大家深入源码来看看其中变化最大——新增的 Graalvm aot 支持。
一、Spring coreSpring framework 6.0 中 Spring core 有了大变更,添加了下列目录:
(资料图)
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文件。
相关阅读
-
世界热推荐:今晚7:00直播丨下一个突破...
今晚19:00,Cocos视频号直播马上点击【预约】啦↓↓↓在运营了三年... -
NFT周刊|Magic Eden宣布支持Polygon网...
Block-986在NFT这样的市场,每周都会有相当多项目起起伏伏。在过去... -
环球今亮点!头条观察 | DeFi的兴衰与...
在比特币得到机构关注之后,许多财务专家预测世界将因为加密货币的... -
重新审视合作,体育Crypto的可靠关系才能双赢
Block-987即使在体育Crypto领域,人们的目光仍然集中在FTX上。随着... -
简讯:前端单元测试,更进一步
前端测试@2022如果从2014年Jest的第一个版本发布开始计算,前端开发... -
焦点热讯:刘强东这波操作秀
近日,刘强东发布京东全员信,信中提到:自2023年1月1日起,逐步为...