|
| 1 | +/* |
| 2 | + * Copyright 2002-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.context.aot; |
| 18 | + |
| 19 | +import java.util.function.BiConsumer; |
| 20 | +import java.util.function.BiFunction; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.springframework.aot.test.generate.TestGenerationContext; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; |
| 27 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 28 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 29 | +import org.springframework.context.ApplicationContextInitializer; |
| 30 | +import org.springframework.context.annotation.AnnotationConfigUtils; |
| 31 | +import org.springframework.context.support.GenericApplicationContext; |
| 32 | +import org.springframework.core.test.tools.Compiled; |
| 33 | +import org.springframework.core.test.tools.TestCompiler; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests for <a href="https://github.com/spring-projects/spring-framework/issues/35871">gh-35871</a>. |
| 39 | + * |
| 40 | + * <p>Verifies that {@code getBean(name, args)} produces beans with the same |
| 41 | + * observable state in both regular and AOT modes. In AOT mode, an |
| 42 | + * {@code InstanceSupplier} with {@code andThen()} post-processing is generated |
| 43 | + * for {@code @Autowired} setter injection. When explicit constructor args |
| 44 | + * bypass the instance supplier, the post-processing must still be applied. |
| 45 | + */ |
| 46 | +class PrototypeWithArgsAotTests { |
| 47 | + |
| 48 | + @Test |
| 49 | + void prototypeWithAutowiredSetterAndExplicitArgs() { |
| 50 | + assertAotAndRegularModesProduceSameResult((ctx, args) -> { |
| 51 | + PrototypeService instance = (PrototypeService) ctx.getBean("prototypeService", args); |
| 52 | + return new BeanSnapshot(instance.getName(), instance.getSingletonService() != null); |
| 53 | + }, "testName"); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Sets up a context with a prototype bean that has an {@code @Autowired} |
| 58 | + * setter, then asserts that the given extractor produces the same result |
| 59 | + * in regular mode and in AOT-compiled mode. |
| 60 | + */ |
| 61 | + private static void assertAotAndRegularModesProduceSameResult( |
| 62 | + BiFunction<GenericApplicationContext, Object[], BeanSnapshot> extractor, Object... args) { |
| 63 | + |
| 64 | + // Regular mode |
| 65 | + GenericApplicationContext regularContext = createApplicationContext(); |
| 66 | + regularContext.refresh(); |
| 67 | + BeanSnapshot regularResult = extractor.apply(regularContext, args); |
| 68 | + regularContext.close(); |
| 69 | + |
| 70 | + // AOT mode |
| 71 | + GenericApplicationContext aotSource = createApplicationContext(); |
| 72 | + testCompiledResult(aotSource, (initializer, compiled) -> { |
| 73 | + GenericApplicationContext aotContext = toFreshApplicationContext(initializer); |
| 74 | + BeanSnapshot aotResult = extractor.apply(aotContext, args); |
| 75 | + assertThat(aotResult) |
| 76 | + .as("AOT mode should produce the same result as regular mode") |
| 77 | + .isEqualTo(regularResult); |
| 78 | + aotContext.close(); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + private static GenericApplicationContext createApplicationContext() { |
| 83 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 84 | + context.registerBeanDefinition( |
| 85 | + AnnotationConfigUtils.AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME, |
| 86 | + new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class)); |
| 87 | + context.registerBeanDefinition("singletonService", |
| 88 | + new RootBeanDefinition(SingletonService.class)); |
| 89 | + RootBeanDefinition prototypeDef = new RootBeanDefinition(PrototypeService.class); |
| 90 | + prototypeDef.setScope(BeanDefinition.SCOPE_PROTOTYPE); |
| 91 | + context.registerBeanDefinition("prototypeService", prototypeDef); |
| 92 | + return context; |
| 93 | + } |
| 94 | + |
| 95 | + private static TestGenerationContext processAheadOfTime(GenericApplicationContext applicationContext) { |
| 96 | + ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator(); |
| 97 | + TestGenerationContext generationContext = new TestGenerationContext(); |
| 98 | + generator.processAheadOfTime(applicationContext, generationContext); |
| 99 | + generationContext.writeGeneratedContent(); |
| 100 | + return generationContext; |
| 101 | + } |
| 102 | + |
| 103 | + private static void testCompiledResult(GenericApplicationContext applicationContext, |
| 104 | + BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) { |
| 105 | + testCompiledResult(processAheadOfTime(applicationContext), result); |
| 106 | + } |
| 107 | + |
| 108 | + @SuppressWarnings("unchecked") |
| 109 | + private static void testCompiledResult(TestGenerationContext generationContext, |
| 110 | + BiConsumer<ApplicationContextInitializer<GenericApplicationContext>, Compiled> result) { |
| 111 | + TestCompiler.forSystem().with(generationContext).compile(compiled -> |
| 112 | + result.accept(compiled.getInstance(ApplicationContextInitializer.class), compiled)); |
| 113 | + } |
| 114 | + |
| 115 | + private static GenericApplicationContext toFreshApplicationContext( |
| 116 | + ApplicationContextInitializer<GenericApplicationContext> initializer) { |
| 117 | + GenericApplicationContext freshApplicationContext = new GenericApplicationContext(); |
| 118 | + initializer.initialize(freshApplicationContext); |
| 119 | + freshApplicationContext.refresh(); |
| 120 | + return freshApplicationContext; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + record BeanSnapshot(String name, boolean hasInjectedDependency) { |
| 125 | + } |
| 126 | + |
| 127 | + public static class SingletonService { |
| 128 | + } |
| 129 | + |
| 130 | + public static class PrototypeService { |
| 131 | + |
| 132 | + private final String name; |
| 133 | + |
| 134 | + private SingletonService singletonService; |
| 135 | + |
| 136 | + public PrototypeService(String name) { |
| 137 | + this.name = name; |
| 138 | + } |
| 139 | + |
| 140 | + public String getName() { |
| 141 | + return name; |
| 142 | + } |
| 143 | + |
| 144 | + public SingletonService getSingletonService() { |
| 145 | + return singletonService; |
| 146 | + } |
| 147 | + |
| 148 | + @Autowired |
| 149 | + public void setSingletonService(SingletonService singletonService) { |
| 150 | + this.singletonService = singletonService; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments