diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotInitializerNotFoundException.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotInitializerNotFoundException.java new file mode 100644 index 0000000000..1bd8d4f6fc --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotInitializerNotFoundException.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot; + +/** + * Exception thrown when the AOT initializer couldn't be found. + * + * @author Moritz Halbritter + * @since 3.2.6 + */ +public class AotInitializerNotFoundException extends RuntimeException { + + private final Class mainClass; + + public AotInitializerNotFoundException(Class mainClass, String initializerClassName) { + super("Startup with AOT mode enabled failed: AOT initializer %s could not be found" + .formatted(initializerClassName)); + this.mainClass = mainClass; + } + + public Class getMainClass() { + return this.mainClass; + } + +} diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 01277d0dfb..480d9a94d3 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -439,10 +439,9 @@ public class SpringApplication { initializers.stream().filter(AotApplicationContextInitializer.class::isInstance).toList()); if (aotInitializers.isEmpty()) { String initializerClassName = this.mainApplicationClass.getName() + "__ApplicationContextInitializer"; - Assert.state(ClassUtils.isPresent(initializerClassName, getClassLoader()), - "You are starting the application with AOT mode enabled but AOT processing hasn't happened. " - + "Please build your application with enabled AOT processing first, " - + "or remove the system property 'spring.aot.enabled' to run the application in regular mode"); + if (!ClassUtils.isPresent(initializerClassName, getClassLoader())) { + throw new AotInitializerNotFoundException(this.mainApplicationClass, initializerClassName); + } aotInitializers.add(AotApplicationContextInitializer.forInitializerClasses(initializerClassName)); } initializers.removeAll(aotInitializers); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/AotInitializerNotFoundFailureAnalyzer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/AotInitializerNotFoundFailureAnalyzer.java new file mode 100644 index 0000000000..ac3db4fdf4 --- /dev/null +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/AotInitializerNotFoundFailureAnalyzer.java @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.diagnostics.analyzer; + +import org.springframework.boot.AotInitializerNotFoundException; +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; + +/** + * An {@link AbstractFailureAnalyzer} that performs analysis of failures caused by a + * {@link AotInitializerNotFoundException}. + * + * @author Moritz Halbritter + */ +class AotInitializerNotFoundFailureAnalyzer extends AbstractFailureAnalyzer { + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, AotInitializerNotFoundException cause) { + return new FailureAnalysis(cause.getMessage(), "Consider the following:\n" + + "\tDid you build the application with enabled AOT processing?\n" + + "\tIs the main class %s correct?\n".formatted(cause.getMainClass().getName()) + + "\tIf you want to run the application in regular mode, remove the system property 'spring.aot.enabled'", + cause); + } + +} diff --git a/spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories index f6cea1c9e6..18bbe901ab 100644 --- a/spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot/src/main/resources/META-INF/spring.factories @@ -64,6 +64,7 @@ org.springframework.boot.diagnostics.FailureAnalyzer=\ org.springframework.boot.context.config.ConfigDataNotFoundFailureAnalyzer,\ org.springframework.boot.context.properties.IncompatibleConfigurationFailureAnalyzer,\ org.springframework.boot.context.properties.NotConstructorBoundInjectionFailureAnalyzer,\ +org.springframework.boot.diagnostics.analyzer.AotInitializerNotFoundFailureAnalyzer,\ org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\ org.springframework.boot.diagnostics.analyzer.BeanDefinitionOverrideFailureAnalyzer,\ org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,\ diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java index e6b2e72f4e..0bce832b57 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/SpringApplicationTests.java @@ -1445,8 +1445,8 @@ class SpringApplicationTests { application.setMainApplicationClass(TestSpringApplication.class); System.setProperty(AotDetector.AOT_ENABLED, "true"); try { - assertThatIllegalStateException().isThrownBy(application::run) - .withMessageContaining("but AOT processing hasn't happened"); + assertThatExceptionOfType(AotInitializerNotFoundException.class).isThrownBy(application::run) + .withMessageMatching("^.+AOT initializer .+ could not be found$"); } finally { System.clearProperty(AotDetector.AOT_ENABLED); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/AotInitializerNotFoundFailureAnalyzerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/AotInitializerNotFoundFailureAnalyzerTests.java new file mode 100644 index 0000000000..500e516f1e --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/AotInitializerNotFoundFailureAnalyzerTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.diagnostics.analyzer; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.AotInitializerNotFoundException; +import org.springframework.boot.diagnostics.FailureAnalysis; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link AotInitializerNotFoundFailureAnalyzer}. + * + * @author Moritz Halbritter + */ +class AotInitializerNotFoundFailureAnalyzerTests { + + @Test + void shouldAnalyze() { + FailureAnalysis analysis = analyze(); + assertThat(analysis.getDescription()).isEqualTo( + "Startup with AOT mode enabled failed: AOT initializer class org.springframework.boot.diagnostics.analyzer.AotInitializerNotFoundFailureAnalyzerTests__ApplicationContextInitializer could not be found"); + assertThat(analysis.getAction()).isEqualTo( + """ + Consider the following: + \tDid you build the application with enabled AOT processing? + \tIs the main class org.springframework.boot.diagnostics.analyzer.AotInitializerNotFoundFailureAnalyzerTests correct? + \tIf you want to run the application in regular mode, remove the system property 'spring.aot.enabled'"""); + } + + private FailureAnalysis analyze() { + return new AotInitializerNotFoundFailureAnalyzer() + .analyze(new AotInitializerNotFoundException(AotInitializerNotFoundFailureAnalyzerTests.class, + AotInitializerNotFoundFailureAnalyzerTests.class + "__ApplicationContextInitializer")); + } + +}