|
16 | 16 |
|
17 | 17 | package org.springframework.boot.web.servlet;
|
18 | 18 |
|
| 19 | +import java.util.function.Consumer; |
| 20 | + |
19 | 21 | import org.junit.jupiter.api.AfterEach;
|
20 | 22 | import org.junit.jupiter.api.Test;
|
21 | 23 |
|
| 24 | +import org.springframework.aot.test.generate.TestGenerationContext; |
22 | 25 | import org.springframework.beans.factory.support.RootBeanDefinition;
|
| 26 | +import org.springframework.context.ApplicationContextInitializer; |
23 | 27 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
24 | 28 | import org.springframework.context.annotation.Configuration;
|
| 29 | +import org.springframework.context.aot.ApplicationContextAotGenerator; |
| 30 | +import org.springframework.context.support.GenericApplicationContext; |
25 | 31 | import org.springframework.core.annotation.AnnotationConfigurationException;
|
| 32 | +import org.springframework.core.test.tools.CompileWithForkedClassLoader; |
| 33 | +import org.springframework.core.test.tools.TestCompiler; |
| 34 | +import org.springframework.javapoet.ClassName; |
26 | 35 |
|
27 | 36 | import static org.assertj.core.api.Assertions.assertThat;
|
28 | 37 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
@@ -121,6 +130,32 @@ void basePackageAndNoBasePackageAreCombinedCorrectly() {
|
121 | 130 | "com.example.foo", "com.example.bar");
|
122 | 131 | }
|
123 | 132 |
|
| 133 | + @Test |
| 134 | + @CompileWithForkedClassLoader |
| 135 | + void processAheadOfTimeDoesNotRegisterServletComponentRegisteringPostProcessor() { |
| 136 | + GenericApplicationContext context = new AnnotationConfigApplicationContext(); |
| 137 | + context.registerBean(BasePackages.class); |
| 138 | + compile(context, (freshContext) -> { |
| 139 | + freshContext.refresh(); |
| 140 | + assertThat(freshContext.getBeansOfType(ServletComponentRegisteringPostProcessor.class)).isEmpty(); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + @SuppressWarnings("unchecked") |
| 145 | + private void compile(GenericApplicationContext context, Consumer<GenericApplicationContext> freshContext) { |
| 146 | + TestGenerationContext generationContext = new TestGenerationContext( |
| 147 | + ClassName.get(getClass().getPackageName(), "TestTarget")); |
| 148 | + ClassName className = new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext); |
| 149 | + generationContext.writeGeneratedContent(); |
| 150 | + TestCompiler.forSystem().with(generationContext).compile((compiled) -> { |
| 151 | + GenericApplicationContext freshApplicationContext = new GenericApplicationContext(); |
| 152 | + ApplicationContextInitializer<GenericApplicationContext> initializer = compiled |
| 153 | + .getInstance(ApplicationContextInitializer.class, className.toString()); |
| 154 | + initializer.initialize(freshApplicationContext); |
| 155 | + freshContext.accept(freshApplicationContext); |
| 156 | + }); |
| 157 | + } |
| 158 | + |
124 | 159 | @Configuration(proxyBeanMethods = false)
|
125 | 160 | @ServletComponentScan({ "com.example.foo", "com.example.bar" })
|
126 | 161 | static class ValuePackages {
|
|
0 commit comments