You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 23, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: spring-native-docs/src/main/asciidoc/aot.adoc
+74-15Lines changed: 74 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,14 @@
1
-
[[build-setup]]
2
-
== Build Setup
3
-
This section covers how to configure your build for <<build-setup-maven,Maven>> or <<build-setup-gradle,Gradle>>.
4
-
You'll also learn more about <<build-setup-modes,the types of sources generated and how they're used>>
1
+
[[aot]]
2
+
== AOT generation
5
3
6
-
[[build-setup-maven]]
7
-
=== Maven
4
+
This section covers AOT (Ahead Of Time) generation plugins, including how to configure your build for <<aot-build-setup-maven,Maven>> or <<aot-build-setup-gradle,Gradle>>.
5
+
You'll also learn more about <<aot-modes,AOT runtime modes>> and more details on the <<aot-engine, AOT engine>>.
6
+
7
+
[[aot-build-setup]]
8
+
=== Build setup
9
+
10
+
[[aot-build-setup-maven]]
11
+
==== Maven
8
12
9
13
The plugin should be declared in your `pom.xml` file:
10
14
@@ -57,12 +61,12 @@ Configuration can be performed if needed within the `<configuration>` element, f
57
61
</configuration>
58
62
----
59
63
60
-
See <<build-setup-configuration>> for a list of the configuration options available.
64
+
See <<aot-build-setup-configuration>> for a list of the configuration options available.
61
65
62
66
63
67
64
-
[[build-setup-gradle]]
65
-
=== Gradle
68
+
[[aot-build-setup-gradle]]
69
+
==== Gradle
66
70
67
71
You can configure the Gradle Spring AOT plugin by declaring first the plugin repositories in your `settings.gradle(.kts)` file:
68
72
@@ -168,12 +172,12 @@ springAot {
168
172
169
173
NOTE: The non-idomatic `property.set(...)` syntax in the Gradle Kotlin DSL is due to https://github.com/gradle/gradle/issues/9268[gradle#9268], feel free to vote for this issue.
170
174
171
-
See <<build-setup-configuration>> for more details on the configuration options.
175
+
See <<aot-build-setup-configuration>> for more details on the configuration options.
172
176
173
177
174
178
175
-
[[build-setup-configuration]]
176
-
=== AOT Configuration
179
+
[[aot-build-setup-configuration]]
180
+
==== AOT configuration
177
181
178
182
The Spring AOT plugins allow you to express opinions about the source generation process.
179
183
Here are all the options available:
@@ -198,7 +202,7 @@ Here are all the options available:
198
202
199
203
* `applicationClass` allows to specify an application class (typically annotated with `@SpringBootApplication`), useful when multiple ones are present.
200
204
201
-
==== Debugging the source generation
205
+
=== Debugging the source generation
202
206
203
207
The Spring AOT plugins spawns a new process to perform the source generation.
204
208
To remote debug this process, you can set a debug System property on the command line; then, the source generation process launches with a listener accepting a remote debugger on port `8000` for Maven or `5005` for Gradle.
Spring AOT inspects an application at build-time and generates an optimized version of it.
306
+
Based on your `@SpringBootApplication`-annotated main class, the AOT engine generates a persistent view of the beans that are going to be contributed at runtime in a way that bean instantiation is as straightforward as possible.
307
+
Additional post-processing of the factory is possible using callbacks.
308
+
For instance, these are used to generate the necessary <<native-hints,reflection configuration>> that GraalVM needs to initialize the context in a native image.
309
+
310
+
The engine goes through the following phases:
311
+
312
+
. Prepare the underlying `BeanFactory` so that the relevant bean definitions are available.
313
+
This typically includes bean definitions model parsing (such as `@Configuration`-annotated classes) as well as any additional post-processing of the bean factory.
314
+
. Code generation based on the prepared `BeanFactory`.
315
+
Each bean definition is handled one by one and the necessary code to instantiate the bean and its necessary runtime semantics (such as primary flag) is generated.
316
+
. Additional processing of the bean factory used to optimize the runtime.
317
+
318
+
[[aot-bean-factory-preparation]]
319
+
==== BeanFactory Preparation
320
+
As the `BeanFactory` is fully prepared at build-time, conditions are also evaluated.
321
+
This has an important difference compared to what a regular Spring Boot application does at runtime.
322
+
For instance, if you want to opt-in or opt-out for certain features, you need to configure the environment used at build time to do so.
323
+
324
+
While certain properties like passwords or url can be changed once the application has been prepared, properties that affect, typically, auto-configurations should be set at build-time.
325
+
326
+
[NOTE]
327
+
====
328
+
Conditions on the runtime environment, such as enabling features based on your chosen cloud platform, will no longer run at runtime.
329
+
====
330
+
331
+
A profile is a special sort of condition so these are also evaluated at build-time.
332
+
It is recommended to avoid the use of profiles as processing them at build-time does not allow you to enable or disable them at runtime anyway.
333
+
If you want to keep using them, they should be enabled at build-time, for instance by adding the `spring.profiles.active` property in `application.properties`.
334
+
335
+
Low-level framework callbacks, such as `BeanDefinitionRegistryPostProcessor` are invoked at build-time to create any additional bean definitions.
336
+
To prevent such a callback to be invoked at runtime again, it is not registered as bean, unless it does not have an `infrastructure` role.
337
+
338
+
[[aot-code-generation]]
339
+
==== Code Generation
340
+
Based on a bean name and a merged `RootBeanDefinition`, the engine identifies a suitable `BeanRegistrationWriter` that is responsible to write the necessary code to instantiate the bean at runtime.
341
+
342
+
It is not expected that projects have to define their own writers, but this could happen for corner-cases.
343
+
Writers are identified via implementations of `BeanRegistrationWriterSupplier`, registered in `META-INF/spring.factories`.
344
+
Suppliers are ordered with a first-win approach, and a {github-tree}/spring-aot/src/main/java/org/springframework/aot/context/bootstrap/generator/bean/DefaultBeanRegistrationWriterSupplier.java[default implementation] with lowest precedence that handles most use cases is provided.
345
+
346
+
NOTE: Explicit care is required if a bean requires privileged access in more than one package.
347
+
This happens typically if the bean use `protected` access and extends from another class in a different package that does the same.
348
+
As a rule of thumb, make sure that each custom bean of yours can be instantiated in a test in a usable form.
349
+
350
+
[[aot-additional-processing]]
351
+
==== Additional Processing
352
+
Additional processing of the `BeanFactory` currently only scans for `@EventListener`-annotated methods, but future versions may provide additional implementations.
353
+
354
+
More core to GraalVM support is the generation of an optimized set of native configuration based on the actual beans of the application, as covered by the next section.
Copy file name to clipboardExpand all lines: spring-native-docs/src/main/asciidoc/executable-jar-to-native.adoc
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,12 +7,12 @@ This can be useful for various use cases:
7
7
* Keep the regular JVM pipeline and turn the JVM Spring Boot application to native on the CI/CD platform.
8
8
* Keep an architecture neutral deployment artifact, as `native-image` https://github.com/oracle/graal/issues/407[does not support cross-compilation].
9
9
10
-
WARNING: A mandatory pre-requisite is to use <<spring-aot>> Maven or Gradle upstream to build the Spring Boot executable JAR.
10
+
WARNING: A mandatory pre-requisite is to use <<aot>> Maven or Gradle upstream to build the Spring Boot executable JAR.
11
11
12
12
=== With Buildpacks
13
13
14
14
Spring Boot applications usually use {buildpacks}[Buildpacks] via the Maven (`mvn spring-boot:build-image`), or Gradle (`gradle bootBuildImage`) integration.
15
-
You can also use directly {buildpacks-docs}/tools/pack/[the `pack` CLI] to turn a Spring Boot executable JAR built with <<spring-aot>> into an optimized container image.
15
+
You can also use directly {buildpacks-docs}/tools/pack/[the `pack` CLI] to turn a Spring Boot executable JAR built with <<aot>> into an optimized container image.
16
16
17
17
First, make sure that a Docker daemon is available, either https://hub.docker.com/search?type=edition&offering=community[locally] or {buildpacks-docs}/app-developer-guide/build-a-windows-app/#using-remote-docker-hosts[remotely].
18
18
You also need to {buildpacks-docs}/tools/pack/[Install `pack`].
@@ -29,7 +29,7 @@ NOTE: This does not require a local `native-image` installation.
29
29
30
30
=== With `native-image`
31
31
32
-
Another option is to turn a Spring Boot executable JAR built with <<spring-aot>> into a native executable using the GraalVM `native-image` compiler.
32
+
Another option is to turn a Spring Boot executable JAR built with <<aot>> into a native executable using the GraalVM `native-image` compiler.
33
33
For this to work, you need to <<getting-started-native-image-system-requirements,Install native-image>>.
34
34
35
35
Assuming a Spring Boot executable JAR built as `my-app-0.0.1-SNAPSHOT.jar` in the `target` directory:
* `spring-aot-gradle-plugin`: Gradle plugin that invokes AOT generation.
47
+
* `spring-aot-maven-plugin`: Maven plugin that invokes AOT generation.
48
48
* `samples`: contains various samples that demonstrate features usage and are used as integration tests.
49
49
50
50
[[getting-started]]
51
-
== Getting Started
52
-
53
-
NOTE: Applications using Spring Native should be compiled with either Java 11 or Java 17.
51
+
== Getting started
54
52
55
53
There are two main ways to build a Spring Boot native application:
56
54
57
55
* Using <<getting-started-buildpacks, Spring Boot Buildpacks support>> to generate a lightweight container containing a native executable.
58
-
* Using <<getting-started-native-image, the GraalVM native image Maven plugin support>> to generate a native executable.
56
+
* Using <<getting-started-native-build-tools, the Native Build Tools>> to generate a native executable.
59
57
60
58
TIP: The easiest way to start a new native Spring Boot project is to go to https://start.spring.io, add the "Spring Native" dependency and generate the project.
0 commit comments