弹簧配置导入参考

时间:2015-11-02 07:32:29

标签: spring import configuration

我有四个spring配置类(用@Configuration注释)。 配置类A定义将在配置类B和C中使用的bean。 配置类D导入其他三个配置类A,B和C.

A类

@Configuration
public Class A {

    @Bean
    public MyBean bean1() {
        return new Bean1();
    }

    @Bean
    public MyBean bean2() {
        return new Bean2();
    }

    @Bean
    public MyBean bean3() {
        return new Bean3();
    }

}

B类

@Configuration
public class B {

    @Autowired
    @Qualifier("bean1")
    private MyBean bean1;

    @Autowired
    @Qualifier("bean2")
    private MyBean bean2;

    @Bean
    public MyBeanCollector beanCollector() {
        MyBeanCollector mbc = new MyBeanCollector();
        mbc.add(bean1);
        mbc.add(bean2);
        return mbc;
    }

}

C类

@Configuration
public class C {

    @Autowired
    @Qualifier("bean1")
    private MyBean bean1;

    @Autowired
    @Qualifier("bean3")
    private MyBean bean3;

    @Bean
    public MyAnotherBeanCollector anotherBeanCollector() {
        MyAnotherBeanCollector mabc = new MyAnotherBeanCollector();
        mabc.add(bean1);
        mabc.add(bean3);
        return mabc;
    }

}

D类

@Configuration
@Import(A.class, B.class, C.class)
public class D {
}

现在,我的问题是,我一直在启动时遇到异常:

org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'beanCollector': Requested bean is currently in creation: Is there an unresolvable circular reference? at         
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:327)

我不确定为什么我一直收到此错误消息。请帮忙。

1 个答案:

答案 0 :(得分:0)

使用您的配置创建了sample project - 工作正常

  1. git clone https://github.com/denis-zhdanov/spring-playground.git
  2. cd spring-playground
  3. git checkout so-33472688
  4. ./ gradlew -PmainClass = org.denis.test.spring.di.SpringStart execute
相关问题