Skip to content

Commit

Permalink
Fix singleton handling of ConfigurationProperties hints
Browse files Browse the repository at this point in the history
Closes gh-31248
  • Loading branch information
snicoll committed Jun 4, 2022
1 parent cf19c17 commit c996e43
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.core.ResolvableType;
Expand All @@ -64,8 +63,10 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL
String[] beanNames = beanFactory.getBeanNamesForAnnotation(ConfigurationProperties.class);
List<Class<?>> types = new ArrayList<>();
for (String beanName : beanNames) {
BeanDefinition beanDefinition = beanFactory.getMergedBeanDefinition(beanName);
types.add(ClassUtils.getUserClass(beanDefinition.getResolvableType().toClass()));
Class<?> beanType = beanFactory.getType(beanName, false);
if (beanType != null) {
types.add(ClassUtils.getUserClass(beanType));
}
}
if (!CollectionUtils.isEmpty(types)) {
return new ConfigurationPropertiesReflectionHintsContribution(types);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContext;
Expand All @@ -61,7 +62,6 @@ void configurationPropertiesBeanFactoryInitializationAotProcessorIsRegistered()
assertThat(new AotFactoriesLoader(new DefaultListableBeanFactory())
.load(BeanFactoryInitializationAotProcessor.class))
.anyMatch(ConfigurationPropertiesBeanFactoryInitializationAotProcessor.class::isInstance);

}

@Test
Expand All @@ -71,6 +71,15 @@ void processNoMatchesReturnsNullContribution() {
assertThat(this.processor.processAheadOfTime(beanFactory)).isNull();
}

@Test
void processManuallyRegisteredSingleton() {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
beanFactory.registerSingleton("test", new SampleProperties());
RuntimeHints runtimeHints = process(beanFactory);
assertThat(runtimeHints.reflection().typeHints()).singleElement()
.satisfies(javaBeanBinding(SampleProperties.class));
}

@Test
void processJavaBeanConfigurationProperties() {
RuntimeHints runtimeHints = process(SampleProperties.class);
Expand Down Expand Up @@ -245,6 +254,10 @@ private RuntimeHints process(Class<?>... types) {
for (Class<?> type : types) {
beanFactory.registerBeanDefinition(type.getName(), new RootBeanDefinition(type));
}
return process(beanFactory);
}

private RuntimeHints process(ConfigurableListableBeanFactory beanFactory) {
BeanFactoryInitializationAotContribution contribution = this.processor.processAheadOfTime(beanFactory);
assertThat(contribution).isNotNull();
GenerationContext generationContext = new DefaultGenerationContext(new InMemoryGeneratedFiles());
Expand Down

0 comments on commit c996e43

Please sign in to comment.