Some of the changes in Spring 6.0, in addition to some of the dependency package adjustments, it seems like not much change. Today I’ll take you deep into the source code to explore the biggest change - the new Graalvm aot support.

1. Spring core

Spring framework 6.0 has a major change to Spring core with the addition of the following directories.

  • GraalVM feature – GraalVM allows clients to intercept native images to generate and run custom code at different stages of initialization. (The GraalVM APi may change at any time, so it was not included in the framework public API)
  • aot – the core package of Spring AOT infrastructure.
  • javapoet – Java API package for generating Java source code.

Spring framework 6.0

The javapoet directory has only one package-info.java file, which will be packaged into this directory when compiled, with the contents of the square organization’s open source javapoet.

1
2
3
4
5
6
7

/**
 * 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;

The GraalVM feature module is also packaged into the aot/graalvm directory after compilation.

aot/graalvm

In addition, aot.factories file has been added to the resolrces directory.

aot.factories file has been added to the resolrces directory

2. Spring beans

Add Spring bean factories to support graalvm AOT. also add aot directory and aot.factories file.

graalvm AOT

3. Spring context

Add support for application context AOT.

Add support for application context AOT

4. Other modules

The above 3 modules add the aot extension interface, the other modules of Spring framework just need to define the aot.factories file.

Let’s take a look at the aot.factories file configuration and content (spring-web\src\main\resources\META-INF\spring\aot.factories).

1
2
3
4
5
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

Configure the mime.types resource file in HttpMimeTypesRuntimeHints.

1
2
3
4
5
6
7
class HttpMimeTypesRuntimeHints implements RuntimeHintsRegistrar {

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

CodecConfigurerRuntimeHints configures CodecConfigurer.properties and the classes configured in it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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));
 }

}

The contents of the CodecConfigurer.properties file are as follows.

1
2
3
4
5
# 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

5. Summary

Spring framework 6.0 added Graalvm aot support extensions and aot.factories configuration file and specification to complete the extensions for dependencies that do not support Graalvm aot. This requires us to analyze and complete support for Graalvm aot when developing the Spring boot starter. For those that are not supported, such as custom resource files, reflections, etc. use the aot.factories file for configuration.

Before Spring boot 3.0, we needed to introduce spring-native dependencies to extend support for resource files, reflection, etc. when using Graalvm aot.

After Spring boot 3.0 or Spring framework 6.0 we can use the built-in support of Spring framework 6.0 to handle this directly through custom aot.factories file configuration.

Reference https://mp.weixin.qq.com/s/dz77CGmgDh5Zs2ejykY8Aw