Skip to content

Report friendly error when failing to find AOT initializer #38188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
* @author Chris Bono
* @author Moritz Halbritter
* @author Tadaya Tsuyukubo
* @author Yanming Zhou
* @since 1.0.0
* @see #run(Class, String[])
* @see #run(Class[], String[])
Expand Down Expand Up @@ -436,6 +437,12 @@ private void addAotGeneratedInitializerIfNecessary(List<ApplicationContextInitia
initializers.stream().filter(AotApplicationContextInitializer.class::isInstance).toList());
if (aotInitializers.isEmpty()) {
String initializerClassName = this.mainApplicationClass.getName() + "__ApplicationContextInitializer";
if (!ClassUtils.isPresent(initializerClassName, getClassLoader())) {
throw new IllegalArgumentException(
"You are starting application with AOT mode but not AOT-processed,"
+ " please build your application with AOT first."
+ " Or remove system property 'spring.aot.enabled=true' to run as regular mode.");
}
aotInitializers.add(AotApplicationContextInitializer.forInitializerClasses(initializerClassName));
}
initializers.removeAll(aotInitializers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
* @author Sebastien Deleuze
* @author Moritz Halbritter
* @author Tadaya Tsuyukubo
* @author Yanming Zhou
*/
@ExtendWith(OutputCaptureExtension.class)
class SpringApplicationTests {
Expand Down Expand Up @@ -1375,6 +1376,21 @@ void shouldUseAotInitializer() {
}
}

@Test
void shouldReportFriendlyErrorIfAotInitializerNotFound() {
SpringApplication application = new SpringApplication(TestSpringApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.setMainApplicationClass(TestSpringApplication.class);
System.setProperty(AotDetector.AOT_ENABLED, "true");
try {
assertThatIllegalArgumentException().isThrownBy(application::run)
.withMessageContaining(AotDetector.AOT_ENABLED);
}
finally {
System.clearProperty(AotDetector.AOT_ENABLED);
}
}

@Test
void fromRunsWithAdditionalSources() {
assertThat(ExampleAdditionalConfig.local.get()).isNull();
Expand Down