Skip to content

Commit 8126d26

Browse files
quaffmhalbritter
authored andcommitted
Report friendly error when failing to find AOT initializer
See gh-38188
1 parent 8733c8c commit 8126d26

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
* @author Chris Bono
169169
* @author Moritz Halbritter
170170
* @author Tadaya Tsuyukubo
171+
* @author Yanming Zhou
171172
* @since 1.0.0
172173
* @see #run(Class, String[])
173174
* @see #run(Class[], String[])
@@ -436,6 +437,12 @@ private void addAotGeneratedInitializerIfNecessary(List<ApplicationContextInitia
436437
initializers.stream().filter(AotApplicationContextInitializer.class::isInstance).toList());
437438
if (aotInitializers.isEmpty()) {
438439
String initializerClassName = this.mainApplicationClass.getName() + "__ApplicationContextInitializer";
440+
if (!ClassUtils.isPresent(initializerClassName, getClassLoader())) {
441+
throw new IllegalArgumentException(
442+
"You are starting application with AOT mode but not AOT-processed,"
443+
+ " please build your application with AOT first."
444+
+ " Or remove system property 'spring.aot.enabled=true' to run as regular mode.");
445+
}
439446
aotInitializers.add(AotApplicationContextInitializer.forInitializerClasses(initializerClassName));
440447
}
441448
initializers.removeAll(aotInitializers);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@
163163
* @author Sebastien Deleuze
164164
* @author Moritz Halbritter
165165
* @author Tadaya Tsuyukubo
166+
* @author Yanming Zhou
166167
*/
167168
@ExtendWith(OutputCaptureExtension.class)
168169
class SpringApplicationTests {
@@ -1375,6 +1376,21 @@ void shouldUseAotInitializer() {
13751376
}
13761377
}
13771378

1379+
@Test
1380+
void shouldReportFriendlyErrorIfAotInitializerNotFound() {
1381+
SpringApplication application = new SpringApplication(TestSpringApplication.class);
1382+
application.setWebApplicationType(WebApplicationType.NONE);
1383+
application.setMainApplicationClass(TestSpringApplication.class);
1384+
System.setProperty(AotDetector.AOT_ENABLED, "true");
1385+
try {
1386+
assertThatIllegalArgumentException().isThrownBy(application::run)
1387+
.withMessageContaining(AotDetector.AOT_ENABLED);
1388+
}
1389+
finally {
1390+
System.clearProperty(AotDetector.AOT_ENABLED);
1391+
}
1392+
}
1393+
13781394
@Test
13791395
void fromRunsWithAdditionalSources() {
13801396
assertThat(ExampleAdditionalConfig.local.get()).isNull();

0 commit comments

Comments
 (0)