Skip to content

Commit

Permalink
Apply same order than original BPP
Browse files Browse the repository at this point in the history
This commit updates ImportAwareAotBeanPostProcessor to be priority
ordered, with the same order as its original BeanPostProcessor. This
makes sure that infrastructure beans can be post-processed if
necessary

See gh-28915
  • Loading branch information
snicoll committed Aug 4, 2022
1 parent e5f9574 commit 332d4a4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import java.util.Map;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
Expand All @@ -33,7 +35,7 @@
* @author Stephane Nicoll
* @since 6.0
*/
public final class ImportAwareAotBeanPostProcessor implements BeanPostProcessor {
public final class ImportAwareAotBeanPostProcessor implements BeanPostProcessor, PriorityOrdered {

private final MetadataReaderFactory metadataReaderFactory;

Expand All @@ -54,6 +56,11 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean;
}

@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE; // match priority of original post processor
}

private void setAnnotationMetadata(ImportAware instance) {
String importingClass = getImportingClassFor(instance);
if (importingClass == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.aot.test.generator.compile.Compiled;
import org.springframework.aot.test.generator.compile.TestCompiler;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
Expand All @@ -42,12 +43,14 @@
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.testfixture.context.generator.annotation.ImportAwareConfiguration;
import org.springframework.context.testfixture.context.generator.annotation.ImportConfiguration;
import org.springframework.core.Ordered;
import org.springframework.core.testfixture.aot.generate.TestGenerationContext;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.javapoet.CodeBlock;
import org.springframework.javapoet.MethodSpec;
import org.springframework.javapoet.ParameterizedTypeName;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
Expand Down Expand Up @@ -110,6 +113,27 @@ void applyToWhenHasImportAwareConfigurationRegistersBeanPostProcessorAfterApplic
});
}

@Test
void applyToWhenHasImportAwareConfigurationRegistersBeanPostProcessorBeforeRegularBeanPostProcessor() {
BeanFactoryInitializationAotContribution contribution = getContribution(
TestImportAwareBeanPostProcessorConfiguration.class);
contribution.applyTo(this.generationContext, this.beanFactoryInitializationCode);
compile((initializer, compiled) -> {
GenericApplicationContext freshContext = new GenericApplicationContext();
DefaultListableBeanFactory freshBeanFactory = freshContext.getDefaultListableBeanFactory();
initializer.accept(freshBeanFactory);
freshBeanFactory.registerBeanDefinition(TestImportAwareBeanPostProcessor.class.getName(),
new RootBeanDefinition(TestImportAwareBeanPostProcessor.class));
RootBeanDefinition bd = new RootBeanDefinition(String.class);
bd.setInstanceSupplier(() -> "test");
freshBeanFactory.registerBeanDefinition("testProcessing", bd);
freshContext.refresh();
assertThat(freshContext.getBean("testProcessing")).isInstanceOfSatisfying(AnnotationMetadata.class, metadata ->
assertThat(metadata.getClassName()).isEqualTo(TestImportAwareBeanPostProcessorConfiguration.class.getName())
);
});
}

@Test
void applyToWhenHasImportAwareConfigurationRegistersHints() {
BeanFactoryInitializationAotContribution contribution = getContribution(
Expand Down Expand Up @@ -178,4 +202,41 @@ public void setImportMetadata(AnnotationMetadata importMetadata) {

}

@Configuration(proxyBeanMethods = false)
@Import(TestImportAwareBeanPostProcessor.class)
static class TestImportAwareBeanPostProcessorConfiguration {

}

static class TestImportAwareBeanPostProcessor implements BeanPostProcessor, ImportAware,
Ordered, InitializingBean {

private AnnotationMetadata metadata;

@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.metadata = importMetadata;
}

@Nullable
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("testProcessing")) {
return this.metadata;
}
return bean;
}

@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE;
}

@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(this.metadata, "Metadata was not injected");
}

}

}

0 comments on commit 332d4a4

Please sign in to comment.