Skip to content

Commit

Permalink
Merge branch '3.0.x'
Browse files Browse the repository at this point in the history
Closes gh-35160
  • Loading branch information
snicoll committed Apr 25, 2023
2 parents 373b887 + b286e5a commit d55cd3b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
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;
Expand Down Expand Up @@ -100,4 +102,13 @@ private void addPackageNames(Collection<String> additionalPackageNames) {

}

static class ServletComponentScanBeanRegistrationExcludeFilter implements BeanRegistrationExcludeFilter {

@Override
public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) {
return BEAN_NAME.equals(registeredBean.getBeanName());
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -121,6 +130,32 @@ void basePackageAndNoBasePackageAreCombinedCorrectly() {
"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 {
Expand Down

0 comments on commit d55cd3b

Please sign in to comment.