このバージョンはまだ開発中であり、まだ安定しているとは見なされていません。最新の安定バージョンについては、Spring Framework 6.2.6 を使用してください! |
JVM AOT キャッシュ
AOT キャッシュは、JEP 483 (英語) を介して Java 24 で導入された JVM 機能であり、Java アプリケーションの起動時間とメモリ使用量を削減できます。AOT キャッシュはクラスデータの共有 (CDS) [Oracle] の自然な進化形です。Spring Framework は CDS キャッシュと AOT キャッシュの両方をサポートしていますが、使用している JVM バージョン(Java 24+)で利用可能な場合は後者を使用することをお勧めします。
この機能を使用するには、アプリケーションの特定のクラスパスに対して AOT キャッシュを作成する必要があります。このキャッシュは、デプロイ済みのインスタンス上、またはアプリケーションのパッケージ化時などに実行されるトレーニング実行中に作成することが可能です。これは、Spring Framework が提供するフックポイントによって、このようなユースケースを容易にするものです。キャッシュが利用可能になったら、ユーザーは JVM フラグを介してキャッシュの使用をオプトインする必要があります。
If you are using Spring Boot, it is highly recommended to leverage its executable JAR unpacking support which is designed to fulfill the class loading requirements of both AOT cache and CDS. |
Creating the cache
An AOT cache can typically be created when the application exits. The Spring Framework provides a mode of operation where the process can exit automatically once the ApplicationContext
has refreshed. In this mode, all non-lazy initialized singletons have been instantiated, and InitializingBean#afterPropertiesSet
callbacks have been invoked; but the lifecycle has not started, and the ContextRefreshedEvent
has not yet been published.
To create the cache during the training run, it is possible to specify the -Dspring.context.exit=onRefresh
JVM flag to start then exit your Spring application once the ApplicationContext
has refreshed:
AOT cache
CDS
# Both commands need to be run with the same classpath
java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -Dspring.context.exit=onRefresh ...
java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot ...
# To create a CDS archive, your JDK/JRE must have a base image
java -XX:ArchiveClassesAtExit=app.jsa -Dspring.context.exit=onRefresh ...
Using the cache
Once the cache file has been created, you can use it to start your application faster:
AOT cache
CDS
# With the same classpath (or a superset) tan the training run
java -XX:AOTCache=app.aot ...
# With the same classpath (or a superset) tan the training run
java -XX:SharedArchiveFile=app.jsa ...
Pay attention to the logs and the startup time to check if the AOT cache is used successfully. To figure out how effective the cache is, you can enable class loading logs by adding an extra attribute: -Xlog:class+load:file=aot-cache.log
. This creates a aot-cache.log
with every attempt to load a class and its source. Classes that are loaded from the cache should have a "shared objects file" source, as shown in the following example:
[0.151s][info][class,load] org.springframework.core.env.EnvironmentCapable source: shared objects file
[0.151s][info][class,load] org.springframework.beans.factory.BeanFactory source: shared objects file
[0.151s][info][class,load] org.springframework.beans.factory.ListableBeanFactory source: shared objects file
[0.151s][info][class,load] org.springframework.beans.factory.HierarchicalBeanFactory source: shared objects file
[0.151s][info][class,load] org.springframework.context.MessageSource source: shared objects file
If the AOT cache can’t be enabled or if you have a large number of classes that are not loaded from the cache, make sure that the following conditions are fulfilled when creating and using the cache:
まったく同じ JVM を使用する必要があります。
The classpath must be specified as a JAR or a list of JARs, and avoid the usage of directories and
*
wildcard characters.JAR のタイムスタンプは保持する必要があります。
When using the cache, the classpath must be the same than the one used to create it, in the same order. Additional JARs or directories can be specified at the end (but won’t be cached).