Skip to content

Commit db8d2d1

Browse files
committed
Backported test for @Autowired @bean method on configuration subclass
See gh-33030
1 parent 12949be commit db8d2d1

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

Diff for: spring-context/src/test/java/org/springframework/context/annotation/configuration/AutowiredConfigurationTests.java

+43-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import org.junit.jupiter.api.Test;
2727

2828
import org.springframework.beans.factory.BeanFactory;
29+
import org.springframework.beans.factory.InitializingBean;
2930
import org.springframework.beans.factory.ObjectFactory;
3031
import org.springframework.beans.factory.annotation.Autowired;
3132
import org.springframework.beans.factory.annotation.Value;
@@ -43,6 +44,7 @@
4344
import org.springframework.core.annotation.AliasFor;
4445
import org.springframework.core.io.ClassPathResource;
4546
import org.springframework.core.io.Resource;
47+
import org.springframework.util.Assert;
4648

4749
import static org.assertj.core.api.Assertions.assertThat;
4850

@@ -91,7 +93,7 @@ void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable() {
9193
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
9294
OptionalAutowiredMethodConfig.class);
9395

94-
assertThat(context.getBeansOfType(Colour.class).isEmpty()).isTrue();
96+
assertThat(context.getBeansOfType(Colour.class)).isEmpty();
9597
assertThat(context.getBean(TestBean.class).getName()).isEmpty();
9698
context.close();
9799
}
@@ -183,14 +185,22 @@ void testValueInjectionWithProviderMethodArguments() {
183185
context.close();
184186
}
185187

188+
@Test
189+
void testValueInjectionWithAccidentalAutowiredAnnotations() {
190+
AnnotationConfigApplicationContext context =
191+
new AnnotationConfigApplicationContext(ValueConfigWithAccidentalAutowiredAnnotations.class);
192+
doTestValueInjection(context);
193+
context.close();
194+
}
195+
186196
private void doTestValueInjection(BeanFactory context) {
187197
System.clearProperty("myProp");
188198

189199
TestBean testBean = context.getBean("testBean", TestBean.class);
190-
assertThat((Object) testBean.getName()).isNull();
200+
assertThat(testBean.getName()).isNull();
191201

192202
testBean = context.getBean("testBean2", TestBean.class);
193-
assertThat((Object) testBean.getName()).isNull();
203+
assertThat(testBean.getName()).isNull();
194204

195205
System.setProperty("myProp", "foo");
196206

@@ -203,10 +213,10 @@ private void doTestValueInjection(BeanFactory context) {
203213
System.clearProperty("myProp");
204214

205215
testBean = context.getBean("testBean", TestBean.class);
206-
assertThat((Object) testBean.getName()).isNull();
216+
assertThat(testBean.getName()).isNull();
207217

208218
testBean = context.getBean("testBean2", TestBean.class);
209-
assertThat((Object) testBean.getName()).isNull();
219+
assertThat(testBean.getName()).isNull();
210220
}
211221

212222
@Test
@@ -281,7 +291,7 @@ public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours
281291
return new TestBean("");
282292
}
283293
else {
284-
return new TestBean(colour.get().toString() + "-" + colours.get().get(0).toString());
294+
return new TestBean(colour.get() + "-" + colours.get().get(0).toString());
285295
}
286296
}
287297
}
@@ -494,6 +504,32 @@ public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String>
494504
}
495505

496506

507+
@Configuration
508+
static class ValueConfigWithAccidentalAutowiredAnnotations implements InitializingBean {
509+
510+
boolean invoked;
511+
512+
@Override
513+
public void afterPropertiesSet() {
514+
Assert.state(!invoked, "Factory method must not get invoked on startup");
515+
}
516+
517+
@Bean @Scope("prototype")
518+
@Autowired
519+
public TestBean testBean(@Value("#{systemProperties[myProp]}") Provider<String> name) {
520+
invoked = true;
521+
return new TestBean(name.get());
522+
}
523+
524+
@Bean @Scope("prototype")
525+
@Autowired
526+
public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String> name2) {
527+
invoked = true;
528+
return new TestBean(name2.get());
529+
}
530+
}
531+
532+
497533
@Configuration
498534
static class PropertiesConfig {
499535

0 commit comments

Comments
 (0)