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

Commit a7d117d

Browse files
committed
Document Spring Boot substitutions
Closes gh-1475
1 parent 4d23ae6 commit a7d117d

9 files changed

+68
-1
lines changed

spring-native/src/main/java/org/springframework/nativex/substitutions/boot/NativeSpringBootVersion.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727

2828
import org.springframework.boot.origin.Origin;
2929

30-
// TODO Work with Boot team on build time invocation of determineSpringBootVersion() and avoid generated code in org.springframework.boot package
30+
/**
31+
* See {@link Target_SpringBootVersion}.
32+
*/
3133
final class NativeSpringBootVersion {
3234

3335
private static String VERSION = determineSpringBootVersion();

spring-native/src/main/java/org/springframework/nativex/substitutions/boot/Target_BeanDefinitionLoader.java

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
import org.springframework.nativex.substitutions.OnlyIfPresent;
1111
import org.springframework.nativex.substitutions.WithAot;
1212

13+
/**
14+
* Why this substitution exists?
15+
* Because BeanDefinitionLoader is package private and need to be referenced from {@link Target_SpringApplication} (SpringApplication substitution).
16+
*
17+
* How this substitution workarounds the problem?
18+
* It provides aliases for some its methods.
19+
*/
1320
@TargetClass(className = "org.springframework.boot.BeanDefinitionLoader", onlyWith = { WithAot.class, OnlyIfPresent.class })
1421
final class Target_BeanDefinitionLoader {
1522

spring-native/src/main/java/org/springframework/nativex/substitutions/boot/Target_DatabaseInitializationDependencyConfigurer.java

+7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
3030
import com.oracle.svm.core.annotate.Substitute;
3131
import com.oracle.svm.core.annotate.TargetClass;
3232

33+
/**
34+
* Why this substitution exists?
35+
* To avoid using SpringFactoriesLoader#loadFactoryNames() in order to use reflection-less variant when possible.
36+
*
37+
* How this substitution workarounds the problem?
38+
* It invokes SpringFactoriesLoader#loadFactories() when possible instead (which is using underneath StaticSpringFactories generated AOT).
39+
*/
3340
@TargetClass(className="org.springframework.boot.sql.init.dependency.DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor", onlyWith = { WithAot.class, OnlyIfPresent.class })
3441
final class Target_DatabaseInitializationDependencyConfigurer {
3542

spring-native/src/main/java/org/springframework/nativex/substitutions/boot/Target_FailureAnalyzers.java

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
import org.springframework.nativex.substitutions.OnlyIfPresent;
2828
import org.springframework.nativex.substitutions.WithAot;
2929

30+
/**
31+
* Why this substitution exists?
32+
* To avoid using SpringFactoriesLoader#loadFactoryNames() in order to use reflection-less variant when possible.
33+
*
34+
* How this substitution workarounds the problem?
35+
* It invokes SpringFactoriesLoader#loadFactories instead (which is using underneath StaticSpringFactories generated AOT).
36+
*/
3037
@TargetClass(className="org.springframework.boot.diagnostics.FailureAnalyzers", onlyWith = { WithAot.class, OnlyIfPresent.class })
3138
final class Target_FailureAnalyzers {
3239

spring-native/src/main/java/org/springframework/nativex/substitutions/boot/Target_JsonParserFactory.java

+10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727
import org.springframework.nativex.substitutions.RemoveYamlSupport;
2828
import org.springframework.util.ClassUtils;
2929

30+
/**
31+
* Why this substitution exists?
32+
* To avoid shipping YamlJsonParser in the native image when Yaml support is disabled via removeYamlSupport Spring AOT flag.
33+
*
34+
* How this substitution workarounds the problem?
35+
* It substitutes JsonParserFactory#getJsonParser with an alternative version without the related if clause when removeYamlSupport is true.
36+
*
37+
* Possible improvements
38+
* Perform all the ClassUtils#isPresent() checks (including Gson one) at build time.
39+
*/
3040
@TargetClass(className = "org.springframework.boot.json.JsonParserFactory", onlyWith = { RemoveYamlSupport.class, OnlyIfPresent.class })
3141
final class Target_JsonParserFactory {
3242

spring-native/src/main/java/org/springframework/nativex/substitutions/boot/Target_SpringApplication.java

+10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535
import org.springframework.nativex.substitutions.WithAot;
3636
import org.springframework.util.StringUtils;
3737

38+
/**
39+
* Why this substitution exists?
40+
* - It provide different codepaths when AOT mode is enabled.
41+
*
42+
* How this substitution workarounds the problem?
43+
* - It skips BeanDefinitionLoader#load() when AOT mode is enabled in order to skip A LOT of runtime infrastructure to be included in the native image
44+
* like AnnotatedBeanDefinitionReader, XmlBeanDefinitionReader, GroovyBeanDefinitionReader and ClassPathBeanDefinitionScanner.
45+
* - It uses org.springframework.aot.ContextBootstrapInitializer (via getSpringFactoriesInstances(ApplicationContextInitializer.class)) when AOT mode
46+
* is enabled to load the precomputed beans.
47+
*/
3848
@TargetClass(className = "org.springframework.boot.SpringApplication", onlyWith = { WithAot.class, OnlyIfPresent.class })
3949
final class Target_SpringApplication {
4050

spring-native/src/main/java/org/springframework/nativex/substitutions/boot/Target_SpringApplicationBannerPrinter.java

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
import org.springframework.core.env.Environment;
2525
import org.springframework.nativex.substitutions.OnlyIfPresent;
2626

27+
/**
28+
* Why this substitution exists?
29+
* To avoid shipping ImageBanner in the native image which is shipping itself a subset of AWT (really bad for the memory footprint).
30+
*
31+
* How this substitution workarounds the problem?
32+
* It disables image banner support and just returns the default one.
33+
*/
2734
@TargetClass(className = "org.springframework.boot.SpringApplicationBannerPrinter", onlyWith = OnlyIfPresent.class)
2835
final class Target_SpringApplicationBannerPrinter {
2936

spring-native/src/main/java/org/springframework/nativex/substitutions/boot/Target_SpringBootVersion.java

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@
2121

2222
import org.springframework.nativex.substitutions.OnlyIfPresent;
2323

24+
/**
25+
* Why this substitution exists?
26+
* SpringBootVersion#determineSpringBootVersion() current implementation is triggering usage of a lot of infrastructure
27+
* (especially via jarFile.getManifest()) to identify the Spring Boot version at runtime.
28+
*
29+
* How this substitution workarounds the problem?
30+
* NativeSpringBootVersion#determineSpringBootVersion() is invoked at build time because it is invoked to populate
31+
* NativeSpringBootVersion#VERSION + NativeSpringBootVersion configured to be initialized at build time in SpringBootHints.
32+
*/
2433
@TargetClass(className = "org.springframework.boot.SpringBootVersion", onlyWith = OnlyIfPresent.class)
2534
final class Target_SpringBootVersion {
2635

spring-native/src/main/java/org/springframework/nativex/substitutions/boot/Target_WebApplicationType.java

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
import org.springframework.nativex.substitutions.OnlyIfPresent;
88
import org.springframework.nativex.substitutions.WithAot;
99

10+
/**
11+
* Why this substitution exists?
12+
* Because WebApplicationType#deduceFromClasspath() is package private and need to be referenced from
13+
* {@link Target_SpringApplication} (SpringApplication substitution).
14+
*
15+
* How this substitution workarounds the problem?
16+
* It provides an alias for this method.
17+
*/
1018
@TargetClass(className = "org.springframework.boot.WebApplicationType", onlyWith = { WithAot.class, OnlyIfPresent.class })
1119
final class Target_WebApplicationType {
1220

0 commit comments

Comments
 (0)