Skip to content

Commit ec83f8b

Browse files
committed
Merge pull request #38188 from quaff
* pr/38188: Polish "Report friendly error when failing to find AOT initializer" Report friendly error when failing to find AOT initializer Closes gh-38188
2 parents 8733c8c + e3210e7 commit ec83f8b

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

Lines changed: 5 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,10 @@ 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+
Assert.state(ClassUtils.isPresent(initializerClassName, getClassLoader()),
441+
"You are starting the application with AOT mode enabled but AOT processing hasn't happened. "
442+
+ "Please build your application with enabled AOT processing first, "
443+
+ "or remove the system property 'spring.aot.enabled' to run the application in regular mode");
439444
aotInitializers.add(AotApplicationContextInitializer.forInitializerClasses(initializerClassName));
440445
}
441446
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+
assertThatIllegalStateException().isThrownBy(application::run)
1387+
.withMessageContaining("but AOT processing hasn't happened");
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)