Requested bean is currently in creation: Is there an unresolvable circular reference?

后端 未结 7 818
夕颜
夕颜 2020-12-08 01:48

i am using spring 3, and i have two beans of view scope:

1- Bean1:

@Component(\"bean1\")
@Scope(\"view\")
public class Bean1 {

@Aut         


        
7条回答
  •  既然无缘
    2020-12-08 02:15

    In general, the most appropriated way to avoid this problem, (also because of better Mockito integration in JUnit) is to use the Setter/Field Injection as described at https://www.baeldung.com/circular-dependencies-in-spring and at https://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html

    @Component("bean1")
    @Scope("view")
    public class Bean1 {
        private Bean2 bean2;
    
        @Autowired
        public void setBean2(Bean2 bean2) {
            this.bean2 = bean2;
        }
    }
    
    @Component("bean2")
    @Scope("view")
    public class Bean2 {
        private Bean1 bean1;
    
        @Autowired
        public void setBean1(Bean1 bean1) {
            this.bean1 = bean1;
        }
    }
    

提交回复
热议问题