UnsatisfiedDependencyException:请求的 bean 当前正在创建无法解析的循环引用

UnsatisfiedDependencyException: Requested bean is currently in creation unresolvable circular reference

我有一个来源 class 显然破坏了集成测试。下面提供了代码。

@Configuration
@Import( EmailageConfiguration.class )
public class EmailageServiceConfiguration {

    private static final String EMAILAGE_ACCOUNT_ID_CONFIG_KEY = "emailage.key";
    private static final String EMAILAGE_API_KEY_CONFIG_KEY = "emailage.secret";

    private final EmailageConfigHolder holder;

    public EmailageServiceConfiguration( final EmailageConfigHolder holder ) {
        this.holder = holder;
    }

    @Bean
    public EmailageConfigHolder emailageConfigHolder( Environment env ) {

//        final EmailageConfigHolder holder = new EmailageConfigHolder();

        holder.setApiKey( env.getRequiredProperty( EMAILAGE_API_KEY_CONFIG_KEY ) );
        holder.setAccountId( env.getRequiredProperty( EMAILAGE_ACCOUNT_ID_CONFIG_KEY ) );

        return holder;
    }

}

提供IT测试,

public class EmailageServiceIT {

    private static final String EMAILAGE_RESPONSE_FILE = "emailage-response.json";

    private final static String EMAILAGE_ENDPOINT = "/emailage";

    private EmailageService service;

    @Rule
    public WireMockRule wireMockRule = new WireMockRule( wireMockConfig().dynamicPort() );

    @Before
    @SuppressWarnings( "resource" )
    public void setup() {
        int port = wireMockRule.port();

        System.setProperty( "emailage.uri", "http://localhost:" + port + EMAILAGE_ENDPOINT );
        System.setProperty( "emailage.key", "123" );
        System.setProperty( "emailage.secret", "123" );
        System.setProperty( "emailage.connection_timeout", "10000" );
        System.setProperty( "emailage.read_timeout", "10000" );
        System.setProperty( "kafka.bootstrap_servers", "localhost" );
        System.setProperty( "kafka.topic", "ella-test-topic" );

        ApplicationContext context = new AnnotationConfigApplicationContext( EmailageServiceConfiguration.class );
        service = context.getBean( EmailageService.class );
    }


 // .........................

}

当我 运行 maven 命令时,我得到错误堆栈,

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailageServiceConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'emailageServiceConfiguration': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at com.ratepay.ella.service.EmailageServiceIT.setup(EmailageServiceIT.java:64)
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'emailageServiceConfiguration': Requested bean is currently in creation: Is there an unresolvable circular reference?
    at com.ratepay.ella.service.EmailageServiceIT.setup(EmailageServiceIT.java:64)

所以,IT测试基本上是在LOC里断的,

ApplicationContext context = new AnnotationConfigApplicationContext( EmailageServiceConfiguration.class );

我还为问题添加了 EmailageConfigHolder class,

@Getter
@Setter
public class EmailageConfigHolder {

    private String apiKey;
    private String accountId;

}

如何更正?

也许我错了,但是

EmailageServiceConfiguration负责Bean的创建EmailageConfigHolder(@Bean注解不就是这么表达的吗)?

那么 spring 应该如何将它传递给应该创建它的对象的构造函数呢?这就是您的循环依赖最有可能源于的地方。

要修复它,请为您的 EmailageServiceConfiguration 创建一个独立的生产者。