<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          aot.factories 是個(gè)啥?| Spring boot 3.0 搶先了解

          共 3691字,需瀏覽 8分鐘

           ·

          2022-10-19 11:29

          在上篇 Spring 6.0 變更 | Spring boot 3.0 搶先了解 中,我們介紹了 Spring 的一些變更。除了部分依賴包調(diào)整,看上去好像變動(dòng)不大,今天筆者帶著大家深入源碼來(lái)看看其中變化最大——新增的 Graalvm aot 支持。

          一、Spring core

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

          1. GraalVM feature —— GraalVM 允許客戶端攔截本機(jī)映像生成并運(yùn)行自定義初始化不同階段的代碼。(GraalVM APi 可能隨時(shí)改變,所以沒(méi)被納入到框架公共 API)
          2. aot —— Spring AOT 基礎(chǔ)設(shè)施的核心包。
          3. javapoet —— 用于生成 Java 源代碼的 Java API 包。

          javapoet 目錄只有一個(gè) package-info.java 文件,編譯時(shí)會(huì)打包到該目錄下,內(nèi)容為 square 組織開(kāi)源的 javapoet。

          /**
           * Spring's repackaging of
           * <a >JavaPoet</a>
           * (with Spring-specific utilities; for internal use only).
           */

          package org.springframework.javapoet;

          GraalVM feature 模塊編譯之后也會(huì)打包到 aot/graalvm 目錄。另外 resolrces 目錄新增了 aot.factories 文件。

          二、Spring beans

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

          二、Spring context

          添加應(yīng)用程序 context  AOT 的支持。

          三、其它模塊

          以上 3 個(gè)模塊添加了 aot 的擴(kuò)展接口,Spring framework 其他的模塊只需要定義  aot.factories 文件。下面我們看看 aot.factories 文件配置和內(nèi)容(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 文件內(nèi)容:

          # 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

          四、總結(jié)

          Spring framework 6.0 添加了 Graalvm aot 支持?jǐn)U展和  aot.factories 配置文件和規(guī)范,可完成對(duì)不支持 Graalvm aot 的依賴進(jìn)行擴(kuò)展。這樣就需要我們?cè)陂_(kāi)發(fā) Spring boot starter 時(shí)分析和完成對(duì) Graalvm aot 的支持情況。對(duì)不支持的,例如:自定義資源文件、反射等使用 aot.factories 文件進(jìn)行配置。 

          在 Spring boot 3.0 之前,我們使用 Graalvm aot 需要引入 spring-native 依賴來(lái)擴(kuò)展對(duì)資源文件、反射等支持。詳見(jiàn):Spring Native 可以正式使用了么?

          在 Spring boot 3.0 或者 Spring framework 6.0 之后我們可以直接使用 Spring framework 6.0 內(nèi)置的支持,通過(guò)自定義 aot.factories 文件配置來(lái)處理。未來(lái) mica-auto 也會(huì)支持使用注解來(lái)生成 aot.factories 文件。


          瀏覽 84
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  色婷婷丁香五月天男人天堂 | 日日操天天操夜夜操 | 一级A片亲子乱中文 | 一级片电影中文字幕 | 亚洲A级片 |