Merge branch '3.0.x'

Closes gh-35160
This commit is contained in:
Stephane Nicoll 2023-04-25 11:26:31 +02:00
commit d55cd3b46f
3 changed files with 49 additions and 0 deletions

View File

@ -22,9 +22,11 @@ import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;
import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
@ -100,4 +102,13 @@ class ServletComponentScanRegistrar implements ImportBeanDefinitionRegistrar {
}
static class ServletComponentScanBeanRegistrationExcludeFilter implements BeanRegistrationExcludeFilter {
@Override
public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) {
return BEAN_NAME.equals(registeredBean.getBeanName());
}
}
}

View File

@ -20,3 +20,6 @@ org.springframework.boot.jackson.JsonComponentModule.JsonComponentBeanFactoryIni
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
org.springframework.boot.context.properties.ConfigurationPropertiesBeanRegistrationAotProcessor,\
org.springframework.boot.jackson.JsonMixinModuleEntriesBeanRegistrationAotProcessor
org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter=\
org.springframework.boot.web.servlet.ServletComponentScanRegistrar.ServletComponentScanBeanRegistrationExcludeFilter

View File

@ -16,13 +16,22 @@
package org.springframework.boot.web.servlet;
import java.util.function.Consumer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.aot.test.generate.TestGenerationContext;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.aot.ApplicationContextAotGenerator;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.annotation.AnnotationConfigurationException;
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.javapoet.ClassName;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@ -121,6 +130,32 @@ class ServletComponentScanRegistrarTests {
"com.example.foo", "com.example.bar");
}
@Test
@CompileWithForkedClassLoader
void processAheadOfTimeDoesNotRegisterServletComponentRegisteringPostProcessor() {
GenericApplicationContext context = new AnnotationConfigApplicationContext();
context.registerBean(BasePackages.class);
compile(context, (freshContext) -> {
freshContext.refresh();
assertThat(freshContext.getBeansOfType(ServletComponentRegisteringPostProcessor.class)).isEmpty();
});
}
@SuppressWarnings("unchecked")
private void compile(GenericApplicationContext context, Consumer<GenericApplicationContext> freshContext) {
TestGenerationContext generationContext = new TestGenerationContext(
ClassName.get(getClass().getPackageName(), "TestTarget"));
ClassName className = new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext);
generationContext.writeGeneratedContent();
TestCompiler.forSystem().with(generationContext).compile((compiled) -> {
GenericApplicationContext freshApplicationContext = new GenericApplicationContext();
ApplicationContextInitializer<GenericApplicationContext> initializer = compiled
.getInstance(ApplicationContextInitializer.class, className.toString());
initializer.initialize(freshApplicationContext);
freshContext.accept(freshApplicationContext);
});
}
@Configuration(proxyBeanMethods = false)
@ServletComponentScan({ "com.example.foo", "com.example.bar" })
static class ValuePackages {