mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Test addAotGeneratedInitializerIfNecessary logic
Closes gh-32555
This commit is contained in:
parent
522cb1c0ea
commit
63f6d872e0
@ -420,9 +420,8 @@ public class SpringApplication {
|
||||
initializers.stream().filter(AotApplicationContextInitializer.class::isInstance).toList());
|
||||
if (aotInitializers.isEmpty()) {
|
||||
String initializerClassName = this.mainApplicationClass.getName() + "__ApplicationContextInitializer";
|
||||
AotApplicationContextInitializer<ConfigurableApplicationContext> initializer = AotApplicationContextInitializer
|
||||
.forInitializerClasses(initializerClassName);
|
||||
aotInitializers.add(initializer);
|
||||
aotInitializers.add(AotApplicationContextInitializer
|
||||
.forInitializerClasses(initializerClassName));
|
||||
}
|
||||
initializers.removeAll(aotInitializers);
|
||||
initializers.addAll(0, aotInitializers);
|
||||
|
@ -41,6 +41,7 @@ import org.mockito.InOrder;
|
||||
import org.mockito.Mockito;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.aot.AotDetector;
|
||||
import org.springframework.aot.hint.RuntimeHints;
|
||||
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
|
||||
import org.springframework.beans.CachedIntrospectionResults;
|
||||
@ -1343,6 +1344,21 @@ class SpringApplicationTests {
|
||||
assertThat(RuntimeHintsPredicates.reflection().onMethod(SpringApplication.class, "load")).rejects(hints);
|
||||
}
|
||||
|
||||
@Test // gh-32555
|
||||
void shouldUseAotInitializer() {
|
||||
SpringApplication application = new SpringApplication(ExampleAotProcessedMainClass.class);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
application.setMainApplicationClass(ExampleAotProcessedMainClass.class);
|
||||
System.setProperty(AotDetector.AOT_ENABLED, "true");
|
||||
try {
|
||||
ApplicationContext context = application.run();
|
||||
assertThat(context.getBean("test")).isEqualTo("test");
|
||||
}
|
||||
finally {
|
||||
System.clearProperty(AotDetector.AOT_ENABLED);
|
||||
}
|
||||
}
|
||||
|
||||
private <S extends AvailabilityState> ArgumentMatcher<ApplicationEvent> isAvailabilityChangeEventWithState(
|
||||
S state) {
|
||||
return (argument) -> (argument instanceof AvailabilityChangeEvent<?>)
|
||||
@ -1891,4 +1907,18 @@ class SpringApplicationTests {
|
||||
|
||||
}
|
||||
|
||||
static class ExampleAotProcessedMainClass {
|
||||
|
||||
}
|
||||
|
||||
static class ExampleAotProcessedMainClass__ApplicationContextInitializer
|
||||
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||
applicationContext.getBeanFactory().registerSingleton("test", "test");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user