Skip to content

Commit 6b62b93

Browse files
committed
Backported test for @Autowired @bean method on configuration subclass
See gh-33030
1 parent 2eed02e commit 6b62b93

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

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

+44-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 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.
@@ -27,6 +27,7 @@
2727
import org.junit.jupiter.api.Test;
2828

2929
import org.springframework.beans.factory.BeanFactory;
30+
import org.springframework.beans.factory.InitializingBean;
3031
import org.springframework.beans.factory.ObjectFactory;
3132
import org.springframework.beans.factory.annotation.Autowired;
3233
import org.springframework.beans.factory.annotation.Value;
@@ -44,6 +45,7 @@
4445
import org.springframework.core.annotation.AliasFor;
4546
import org.springframework.core.io.ClassPathResource;
4647
import org.springframework.core.io.Resource;
48+
import org.springframework.util.Assert;
4749

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

@@ -92,8 +94,8 @@ void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable() {
9294
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
9395
OptionalAutowiredMethodConfig.class);
9496

95-
assertThat(context.getBeansOfType(Colour.class).isEmpty()).isTrue();
96-
assertThat(context.getBean(TestBean.class).getName()).isEqualTo("");
97+
assertThat(context.getBeansOfType(Colour.class)).isEmpty();
98+
assertThat(context.getBean(TestBean.class).getName()).isEmpty();
9799
context.close();
98100
}
99101

@@ -184,14 +186,22 @@ void testValueInjectionWithProviderMethodArguments() {
184186
context.close();
185187
}
186188

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

190200
TestBean testBean = context.getBean("testBean", TestBean.class);
191-
assertThat((Object) testBean.getName()).isNull();
201+
assertThat(testBean.getName()).isNull();
192202

193203
testBean = context.getBean("testBean2", TestBean.class);
194-
assertThat((Object) testBean.getName()).isNull();
204+
assertThat(testBean.getName()).isNull();
195205

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

@@ -204,10 +214,10 @@ private void doTestValueInjection(BeanFactory context) {
204214
System.clearProperty("myProp");
205215

206216
testBean = context.getBean("testBean", TestBean.class);
207-
assertThat((Object) testBean.getName()).isNull();
217+
assertThat(testBean.getName()).isNull();
208218

209219
testBean = context.getBean("testBean2", TestBean.class);
210-
assertThat((Object) testBean.getName()).isNull();
220+
assertThat(testBean.getName()).isNull();
211221
}
212222

213223
@Test
@@ -271,7 +281,7 @@ public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours
271281
return new TestBean("");
272282
}
273283
else {
274-
return new TestBean(colour.get().toString() + "-" + colours.get().get(0).toString());
284+
return new TestBean(colour.get() + "-" + colours.get().get(0).toString());
275285
}
276286
}
277287
}
@@ -484,6 +494,32 @@ public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String>
484494
}
485495

486496

497+
@Configuration
498+
static class ValueConfigWithAccidentalAutowiredAnnotations implements InitializingBean {
499+
500+
boolean invoked;
501+
502+
@Override
503+
public void afterPropertiesSet() {
504+
Assert.state(!invoked, "Factory method must not get invoked on startup");
505+
}
506+
507+
@Bean @Scope("prototype")
508+
@Autowired
509+
public TestBean testBean(@Value("#{systemProperties[myProp]}") Provider<String> name) {
510+
invoked = true;
511+
return new TestBean(name.get());
512+
}
513+
514+
@Bean @Scope("prototype")
515+
@Autowired
516+
public TestBean testBean2(@Value("#{systemProperties[myProp]}") Provider<String> name2) {
517+
invoked = true;
518+
return new TestBean(name2.get());
519+
}
520+
}
521+
522+
487523
@Configuration
488524
static class PropertiesConfig {
489525

0 commit comments

Comments
 (0)