|
17 | 17 | package org.springframework.context.aot;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.lang.reflect.Proxy; |
20 | 21 | import java.util.function.BiConsumer;
|
21 | 22 |
|
22 | 23 | import org.junit.jupiter.api.Test;
|
23 | 24 |
|
24 | 25 | import org.springframework.aot.generate.GeneratedFiles.Kind;
|
| 26 | +import org.springframework.aot.generate.GenerationContext; |
25 | 27 | import org.springframework.aot.hint.MemberCategory;
|
| 28 | +import org.springframework.aot.hint.RuntimeHints; |
26 | 29 | import org.springframework.aot.hint.TypeReference;
|
27 | 30 | import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
|
28 | 31 | import org.springframework.aot.test.generator.compile.Compiled;
|
|
44 | 47 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
45 | 48 | import org.springframework.context.annotation.AnnotationConfigUtils;
|
46 | 49 | import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
|
| 50 | +import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver; |
47 | 51 | import org.springframework.context.support.GenericApplicationContext;
|
48 | 52 | import org.springframework.context.testfixture.context.generator.SimpleComponent;
|
49 | 53 | import org.springframework.context.testfixture.context.generator.annotation.AutowiredComponent;
|
50 | 54 | import org.springframework.context.testfixture.context.generator.annotation.CglibConfiguration;
|
51 | 55 | import org.springframework.context.testfixture.context.generator.annotation.InitDestroyComponent;
|
| 56 | +import org.springframework.context.testfixture.context.generator.annotation.LazyAutowiredFieldComponent; |
| 57 | +import org.springframework.context.testfixture.context.generator.annotation.LazyAutowiredMethodComponent; |
| 58 | +import org.springframework.context.testfixture.context.generator.annotation.LazyConstructorArgumentComponent; |
| 59 | +import org.springframework.context.testfixture.context.generator.annotation.LazyFactoryMethodArgumentComponent; |
| 60 | +import org.springframework.core.env.Environment; |
| 61 | +import org.springframework.core.io.ResourceLoader; |
52 | 62 | import org.springframework.core.testfixture.aot.generate.TestGenerationContext;
|
53 | 63 |
|
54 | 64 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -93,6 +103,87 @@ void processAheadOfTimeWhenHasAutowiring() {
|
93 | 103 | });
|
94 | 104 | }
|
95 | 105 |
|
| 106 | + @Test |
| 107 | + void processAheadOfTimeWhenHasLazyAutowiringOnField() { |
| 108 | + testAutowiredComponent(LazyAutowiredFieldComponent.class, (bean, generationContext) -> { |
| 109 | + Environment environment = bean.getEnvironment(); |
| 110 | + assertThat(environment).isInstanceOf(Proxy.class); |
| 111 | + ResourceLoader resourceLoader = bean.getResourceLoader(); |
| 112 | + assertThat(resourceLoader).isNotInstanceOf(Proxy.class); |
| 113 | + RuntimeHints runtimeHints = generationContext.getRuntimeHints(); |
| 114 | + assertThat(runtimeHints.proxies().jdkProxies()).singleElement().satisfies(proxyHint -> |
| 115 | + assertThat(proxyHint.getProxiedInterfaces()).isEqualTo(TypeReference.listOf( |
| 116 | + environment.getClass().getInterfaces()))); |
| 117 | + |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void processAheadOfTimeWhenHasLazyAutowiringOnMethod() { |
| 123 | + testAutowiredComponent(LazyAutowiredMethodComponent.class, (bean, generationContext) -> { |
| 124 | + Environment environment = bean.getEnvironment(); |
| 125 | + assertThat(environment).isNotInstanceOf(Proxy.class); |
| 126 | + ResourceLoader resourceLoader = bean.getResourceLoader(); |
| 127 | + assertThat(resourceLoader).isInstanceOf(Proxy.class); |
| 128 | + RuntimeHints runtimeHints = generationContext.getRuntimeHints(); |
| 129 | + assertThat(runtimeHints.proxies().jdkProxies()).singleElement().satisfies(proxyHint -> |
| 130 | + assertThat(proxyHint.getProxiedInterfaces()).isEqualTo(TypeReference.listOf( |
| 131 | + resourceLoader.getClass().getInterfaces()))); |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + void processAheadOfTimeWhenHasLazyAutowiringOnConstructor() { |
| 137 | + testAutowiredComponent(LazyConstructorArgumentComponent.class, (bean, generationContext) -> { |
| 138 | + Environment environment = bean.getEnvironment(); |
| 139 | + assertThat(environment).isInstanceOf(Proxy.class); |
| 140 | + ResourceLoader resourceLoader = bean.getResourceLoader(); |
| 141 | + assertThat(resourceLoader).isNotInstanceOf(Proxy.class); |
| 142 | + RuntimeHints runtimeHints = generationContext.getRuntimeHints(); |
| 143 | + assertThat(runtimeHints.proxies().jdkProxies()).singleElement().satisfies(proxyHint -> |
| 144 | + assertThat(proxyHint.getProxiedInterfaces()).isEqualTo(TypeReference.listOf( |
| 145 | + environment.getClass().getInterfaces()))); |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void processAheadOfTimeWhenHasLazyAutowiringOnFactoryMethod() { |
| 151 | + RootBeanDefinition bd = new RootBeanDefinition(LazyFactoryMethodArgumentComponent.class); |
| 152 | + bd.setFactoryMethodName("of"); |
| 153 | + testAutowiredComponent(LazyFactoryMethodArgumentComponent.class, bd, (bean, generationContext) -> { |
| 154 | + Environment environment = bean.getEnvironment(); |
| 155 | + assertThat(environment).isInstanceOf(Proxy.class); |
| 156 | + ResourceLoader resourceLoader = bean.getResourceLoader(); |
| 157 | + assertThat(resourceLoader).isNotInstanceOf(Proxy.class); |
| 158 | + RuntimeHints runtimeHints = generationContext.getRuntimeHints(); |
| 159 | + assertThat(runtimeHints.proxies().jdkProxies()).singleElement().satisfies(proxyHint -> |
| 160 | + assertThat(proxyHint.getProxiedInterfaces()).isEqualTo(TypeReference.listOf( |
| 161 | + environment.getClass().getInterfaces()))); |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + private <T> void testAutowiredComponent(Class<T> type, BiConsumer<T, GenerationContext> assertions) { |
| 166 | + testAutowiredComponent(type, new RootBeanDefinition(type), assertions); |
| 167 | + } |
| 168 | + |
| 169 | + private <T> void testAutowiredComponent(Class<T> type, RootBeanDefinition beanDefinition, |
| 170 | + BiConsumer<T, GenerationContext> assertions) { |
| 171 | + GenericApplicationContext applicationContext = new GenericApplicationContext(); |
| 172 | + applicationContext.getDefaultListableBeanFactory().setAutowireCandidateResolver( |
| 173 | + new ContextAnnotationAutowireCandidateResolver()); |
| 174 | + applicationContext.registerBeanDefinition(AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME, |
| 175 | + BeanDefinitionBuilder |
| 176 | + .rootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class) |
| 177 | + .setRole(BeanDefinition.ROLE_INFRASTRUCTURE).getBeanDefinition()); |
| 178 | + applicationContext.registerBeanDefinition("testComponent", beanDefinition); |
| 179 | + TestGenerationContext generationContext = processAheadOfTime(applicationContext); |
| 180 | + testCompiledResult(generationContext, (initializer, compiled) -> { |
| 181 | + GenericApplicationContext freshApplicationContext = toFreshApplicationContext(initializer); |
| 182 | + assertThat(freshApplicationContext.getBeanDefinitionNames()).containsOnly("testComponent"); |
| 183 | + assertions.accept(freshApplicationContext.getBean("testComponent", type), generationContext); |
| 184 | + }); |
| 185 | + } |
| 186 | + |
96 | 187 | @Test
|
97 | 188 | void processAheadOfTimeWhenHasInitDestroyMethods() {
|
98 | 189 | GenericApplicationContext applicationContext = new GenericApplicationContext();
|
@@ -189,10 +280,14 @@ private static TestGenerationContext processAheadOfTime(GenericApplicationContex
|
189 | 280 | return generationContext;
|
190 | 281 | }
|
191 | 282 |
|
192 |
| - @SuppressWarnings({ "rawtypes", "unchecked" }) |
193 | 283 | private void testCompiledResult(GenericApplicationContext applicationContext,
|
194 | 284 | BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) {
|
195 |
| - TestGenerationContext generationContext = processAheadOfTime(applicationContext); |
| 285 | + testCompiledResult(processAheadOfTime(applicationContext), result); |
| 286 | + } |
| 287 | + |
| 288 | + @SuppressWarnings({ "rawtypes", "unchecked" }) |
| 289 | + private void testCompiledResult(TestGenerationContext generationContext, |
| 290 | + BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) { |
196 | 291 | TestCompiler.forSystem().withFiles(generationContext.getGeneratedFiles()).compile(compiled ->
|
197 | 292 | result.accept(compiled.getInstance(ApplicationContextInitializer.class), compiled));
|
198 | 293 | }
|
|
0 commit comments