Prevent ServletComponentScan to run again with AOT
Closes gh-34563
This commit is contained in:
parent
8a43710173
commit
b286e5a0bb
|
@ -22,9 +22,11 @@ import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||||
|
import org.springframework.beans.factory.support.RegisteredBean;
|
||||||
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
import org.springframework.core.annotation.AnnotationAttributes;
|
import org.springframework.core.annotation.AnnotationAttributes;
|
||||||
import org.springframework.core.type.AnnotationMetadata;
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,3 +20,6 @@ org.springframework.boot.jackson.JsonComponentModule.JsonComponentBeanFactoryIni
|
||||||
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
|
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
|
||||||
org.springframework.boot.context.properties.ConfigurationPropertiesBeanRegistrationAotProcessor,\
|
org.springframework.boot.context.properties.ConfigurationPropertiesBeanRegistrationAotProcessor,\
|
||||||
org.springframework.boot.jackson.JsonMixinModuleEntriesBeanRegistrationAotProcessor
|
org.springframework.boot.jackson.JsonMixinModuleEntriesBeanRegistrationAotProcessor
|
||||||
|
|
||||||
|
org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter=\
|
||||||
|
org.springframework.boot.web.servlet.ServletComponentScanRegistrar.ServletComponentScanBeanRegistrationExcludeFilter
|
|
@ -16,13 +16,22 @@
|
||||||
|
|
||||||
package org.springframework.boot.web.servlet;
|
package org.springframework.boot.web.servlet;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.aot.test.generate.TestGenerationContext;
|
||||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
import org.springframework.context.ApplicationContextInitializer;
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Configuration;
|
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.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.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||||
|
@ -121,6 +130,32 @@ class ServletComponentScanRegistrarTests {
|
||||||
"com.example.foo", "com.example.bar");
|
"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)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@ServletComponentScan({ "com.example.foo", "com.example.bar" })
|
@ServletComponentScan({ "com.example.foo", "com.example.bar" })
|
||||||
static class ValuePackages {
|
static class ValuePackages {
|
||||||
|
|
Loading…
Reference in New Issue