From db8d2d1626627152b3a62e16a838e5a92f4940b9 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 17 Jun 2024 19:06:49 +0200 Subject: [PATCH] Backported test for @Autowired @Bean method on configuration subclass See gh-33030 --- .../AutowiredConfigurationTests.java | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java index 9a979547657..00cd00e3b8c 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import jakarta.inject.Provider; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -43,6 +44,7 @@ import org.springframework.context.support.GenericApplicationContext; import org.springframework.core.annotation.AliasFor; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.util.Assert; import static org.assertj.core.api.Assertions.assertThat; @@ -91,7 +93,7 @@ class AutowiredConfigurationTests { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( OptionalAutowiredMethodConfig.class); - assertThat(context.getBeansOfType(Colour.class).isEmpty()).isTrue(); + assertThat(context.getBeansOfType(Colour.class)).isEmpty(); assertThat(context.getBean(TestBean.class).getName()).isEmpty(); context.close(); } @@ -183,14 +185,22 @@ class AutowiredConfigurationTests { context.close(); } + @Test + void testValueInjectionWithAccidentalAutowiredAnnotations() { + AnnotationConfigApplicationContext context = + new AnnotationConfigApplicationContext(ValueConfigWithAccidentalAutowiredAnnotations.class); + doTestValueInjection(context); + context.close(); + } + private void doTestValueInjection(BeanFactory context) { System.clearProperty("myProp"); TestBean testBean = context.getBean("testBean", TestBean.class); - assertThat((Object) testBean.getName()).isNull(); + assertThat(testBean.getName()).isNull(); testBean = context.getBean("testBean2", TestBean.class); - assertThat((Object) testBean.getName()).isNull(); + assertThat(testBean.getName()).isNull(); System.setProperty("myProp", "foo"); @@ -203,10 +213,10 @@ class AutowiredConfigurationTests { System.clearProperty("myProp"); testBean = context.getBean("testBean", TestBean.class); - assertThat((Object) testBean.getName()).isNull(); + assertThat(testBean.getName()).isNull(); testBean = context.getBean("testBean2", TestBean.class); - assertThat((Object) testBean.getName()).isNull(); + assertThat(testBean.getName()).isNull(); } @Test @@ -281,7 +291,7 @@ class AutowiredConfigurationTests { return new TestBean(""); } else { - return new TestBean(colour.get().toString() + "-" + colours.get().get(0).toString()); + return new TestBean(colour.get() + "-" + colours.get().get(0).toString()); } } } @@ -494,6 +504,32 @@ class AutowiredConfigurationTests { } + @Configuration + static class ValueConfigWithAccidentalAutowiredAnnotations implements InitializingBean { + + boolean invoked; + + @Override + public void afterPropertiesSet() { + Assert.state(!invoked, "Factory method must not get invoked on startup"); + } + + @Bean @Scope("prototype") + @Autowired + public TestBean testBean(@Value("#{systemProperties[myProp]}") Provider name) { + invoked = true; + return new TestBean(name.get()); + } + + @Bean @Scope("prototype") + @Autowired + public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider name2) { + invoked = true; + return new TestBean(name2.get()); + } + } + + @Configuration static class PropertiesConfig {