-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Closed
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchesAn issue that has been backported to maintenance branchestype: bugA general bugA general bug
Milestone
Description
Affects: 5.3.28
Below is a test that is able to reproduce the issue.
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import static org.assertj.core.api.Assertions.assertThat;
public class NotRequiredIssue {
@Test
void test() {
AnnotationConfigApplicationContext ctx
= new AnnotationConfigApplicationContext(Bean1.class, Bean2Factory.class);
Bean1 bean1A = ctx.getBean(Bean1.class);
// Bean2Factory.bean2() returns null initially, so Bean1.bean2 got null injected
assertThat(bean1A.bean2).isNull();
Bean2 bean2 = new Bean2();
Bean2Factory bean2Factory = ctx.getBean(Bean2Factory.class);
// ask Bean2Factory.bean2() to return non-null instance
bean2Factory.bean2 = bean2;
Bean1 bean1B = ctx.getBean(Bean1.class);
// expects Bean1.bean2 is non-null, however, still got null
assertThat(bean1B.bean2).isNotNull();
assertThat(bean1B.bean2).isSameAs(bean2);
ctx.close();
}
@Scope("prototype")
static class Bean1 {
@Autowired(required = false) // required=false, otherwise will get exception
Bean2 bean2;
}
static class Bean2 {
}
static class Bean2Factory {
Bean2 bean2;
@Scope("prototype") @Bean
Bean2 bean2() {
return this.bean2;
}
}
}
Metadata
Metadata
Assignees
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchesAn issue that has been backported to maintenance branchestype: bugA general bugA general bug