Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit 246f4fb

Browse files
committed
Improve reference documentation structure
- Merge AOT and build setup documentation. - Various updates and fixes. Closes gh-1467
1 parent 481267e commit 246f4fb

File tree

6 files changed

+86
-83
lines changed

6 files changed

+86
-83
lines changed

spring-native-docs/src/main/asciidoc/build-setup.adoc renamed to spring-native-docs/src/main/asciidoc/aot.adoc

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff 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
53

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
812

913
The plugin should be declared in your `pom.xml` file:
1014

@@ -57,12 +61,12 @@ Configuration can be performed if needed within the `<configuration>` element, f
5761
</configuration>
5862
----
5963

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.
6165

6266

6367

64-
[[build-setup-gradle]]
65-
=== Gradle
68+
[[aot-build-setup-gradle]]
69+
==== Gradle
6670

6771
You can configure the Gradle Spring AOT plugin by declaring first the plugin repositories in your `settings.gradle(.kts)` file:
6872

@@ -168,12 +172,12 @@ springAot {
168172

169173
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.
170174

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.
172176

173177

174178

175-
[[build-setup-configuration]]
176-
=== AOT Configuration
179+
[[aot-build-setup-configuration]]
180+
==== AOT configuration
177181

178182
The Spring AOT plugins allow you to express opinions about the source generation process.
179183
Here are all the options available:
@@ -198,7 +202,7 @@ Here are all the options available:
198202

199203
* `applicationClass` allows to specify an application class (typically annotated with `@SpringBootApplication`), useful when multiple ones are present.
200204

201-
==== Debugging the source generation
205+
=== Debugging the source generation
202206

203207
The Spring AOT plugins spawns a new process to perform the source generation.
204208
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.
@@ -237,8 +241,8 @@ $ ./gradlew generateAot -Dorg.gradle.debug=true --no-daemon
237241
----
238242

239243

240-
[[build-setup-modes]]
241-
=== AOT Modes
244+
[[aot-modes]]
245+
=== AOT runtime modes
242246

243247
The generated sources are automatically used by the native image compilation, but are not used by default when running your application with a JVM.
244248
This means that running the application or its tests from the IDE or the command line will not involve those classes.
@@ -293,3 +297,58 @@ tasks.getByName<BootRun>("bootRun") {
293297
systemProperty("springAot", "true")
294298
}
295299
----
300+
301+
302+
[[aot-engine]]
303+
=== AOT engine
304+
305+
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.

spring-native-docs/src/main/asciidoc/executable-jar-to-native.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ This can be useful for various use cases:
77
* Keep the regular JVM pipeline and turn the JVM Spring Boot application to native on the CI/CD platform.
88
* Keep an architecture neutral deployment artifact, as `native-image` https://github.com/oracle/graal/issues/407[does not support cross-compilation].
99

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.
1111

1212
=== With Buildpacks
1313

1414
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.
1616

1717
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].
1818
You also need to {buildpacks-docs}/tools/pack/[Install `pack`].
@@ -29,7 +29,7 @@ NOTE: This does not require a local `native-image` installation.
2929

3030
=== With `native-image`
3131

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.
3333
For this to work, you need to <<getting-started-native-image-system-requirements,Install native-image>>.
3434

3535
Assuming a Spring Boot executable JAR built as `my-app-0.0.1-SNAPSHOT.jar` in the `target` directory:

spring-native-docs/src/main/asciidoc/how-to-contribute.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ It is fine to use reflection in a native world but it is most optimal to do it i
5959
* In the static block/fields of a class initialized at build-time.
6060
* In an AOT transformation run as a Spring AOT build plugin.
6161

62-
NOTE: More guidelines will be provided here as <<spring-aot>> matures.
62+
NOTE: More guidelines will be provided here as <<aot>> matures.
6363

6464
[[how-to-contribute-new-hints]]
6565
=== Contributing new hints

spring-native-docs/src/main/asciidoc/index.adoc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,19 @@ Spring Native is composed of the following modules:
4141
* `spring-native-configuration`: configuration hints for Spring classes used by Spring AOT plugins, including various Spring Boot auto-configurations.
4242
* `spring-native-docs`: reference guide, in asciidoc format.
4343
* `spring-native-tools`: tools used for reviewing image building configuration and output.
44-
* `spring-aot`: AOT transformation infrastructure common to Maven and Gradle plugins.
45-
* `spring-aot-test`: Test-specific AOT transformation infrastructure.
46-
* `spring-aot-gradle-plugin`: Gradle plugin that invokes AOT transformations.
47-
* `spring-aot-maven-plugin`: Maven plugin that invokes AOT transformations.
44+
* `spring-aot`: AOT generation infrastructure common to Maven and Gradle plugins.
45+
* `spring-aot-test`: Test-specific AOT generation infrastructure.
46+
* `spring-aot-gradle-plugin`: Gradle plugin that invokes AOT generation.
47+
* `spring-aot-maven-plugin`: Maven plugin that invokes AOT generation.
4848
* `samples`: contains various samples that demonstrate features usage and are used as integration tests.
4949

5050
[[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
5452

5553
There are two main ways to build a Spring Boot native application:
5654

5755
* 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.
5957

6058
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.
6159

@@ -65,12 +63,10 @@ include::getting-started-native-build-tools.adoc[]
6563

6664
include::support.adoc[]
6765

68-
include::spring-aot.adoc[]
66+
include::aot.adoc[]
6967

7068
include::native-hints.adoc[]
7169

72-
include::build-setup.adoc[]
73-
7470
include::samples.adoc[]
7571

7672
include::native-image-options.adoc[]

spring-native-docs/src/main/asciidoc/spring-aot.adoc

Lines changed: 0 additions & 52 deletions
This file was deleted.

spring-native-docs/src/main/asciidoc/troubleshooting.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Error: Classes that should be initialized at run time got initialized during ima
2727
----
2828

2929
You have probably tried to compile a Spring Boot application to native without the `spring-native` dependency and Spring AOT plugin.
30-
See related <<getting-started-native-image>> and <<getting-started-buildpacks>> documentation.
30+
See related <<getting-started-native-build-tools>> and <<getting-started-buildpacks>> documentation.
3131

3232
==== WARNING: Could not register reflection metadata
3333

0 commit comments

Comments
 (0)