Browse Source

Merge branch '3.4.x' into 3.5.x

Closes gh-48475
3.5.x
Stéphane Nicoll 1 week ago
parent
commit
0578bc5cb7
  1. 8
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListener.java
  2. 22
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListenerTests.java

8
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListener.java vendored

@ -24,7 +24,9 @@ import javax.lang.model.element.Modifier; @@ -24,7 +24,9 @@ import javax.lang.model.element.Modifier;
import org.springframework.aot.AotDetector;
import org.springframework.aot.generate.GeneratedClass;
import org.springframework.aot.generate.GeneratedTypeReference;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.beans.BeanInstantiationException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
@ -59,7 +61,7 @@ import org.springframework.util.ObjectUtils; @@ -59,7 +61,7 @@ import org.springframework.util.ObjectUtils;
*/
public class EnvironmentPostProcessorApplicationListener implements SmartApplicationListener, Ordered {
private static final String AOT_FEATURE_NAME = "EnvironmentPostProcessor";
static final String AOT_FEATURE_NAME = "EnvironmentPostProcessor";
/**
* The default order for the processor.
@ -234,6 +236,10 @@ public class EnvironmentPostProcessorApplicationListener implements SmartApplica @@ -234,6 +236,10 @@ public class EnvironmentPostProcessorApplicationListener implements SmartApplica
method.addParameter(SpringApplication.class, "application");
method.addCode(generateActiveProfilesInitializeCode());
});
generationContext.getRuntimeHints()
.reflection()
.registerType(GeneratedTypeReference.of(generatedClass.getName()),
MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS);
}
private CodeBlock generateActiveProfilesInitializeCode() {

22
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/env/EnvironmentPostProcessorApplicationListenerTests.java vendored

@ -35,6 +35,10 @@ import org.junit.jupiter.api.Test; @@ -35,6 +35,10 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.aot.AotDetector;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.TypeReference;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@ -209,6 +213,24 @@ class EnvironmentPostProcessorApplicationListenerTests { @@ -209,6 +213,24 @@ class EnvironmentPostProcessorApplicationListenerTests {
});
}
@Test
void aotContributionRegistersReflectionHints() {
GenericApplicationContext applicationContext = new GenericApplicationContext();
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles("one", "two");
applicationContext.getBeanFactory().registerSingleton("environment", environment);
BeanFactoryInitializationAotContribution aotContribution = new EnvironmentBeanFactoryInitializationAotProcessor()
.processAheadOfTime(applicationContext.getBeanFactory());
assertThat(aotContribution).isNotNull();
GenerationContext generationContext = new TestGenerationContext();
aotContribution.applyTo(generationContext, null);
assertThat(RuntimeHintsPredicates.reflection()
.onType(TypeReference.of(TestGenerationContext.TEST_TARGET + "__"
+ EnvironmentPostProcessorApplicationListener.AOT_FEATURE_NAME))
.withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
.accepts(generationContext.getRuntimeHints());
}
@Test
void shouldUseAotEnvironmentPostProcessor() {
SpringApplication application = new SpringApplication(ExampleAotProcessedApp.class);

Loading…
Cancel
Save