不满意的DepententyencyException:当前所请求的BEAN正在创建不可避免的圆形参考



我有一个源类,显然打破了集成测试。代码如下提供。

@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类,

@Getter
@Setter
public class EmailageConfigHolder {
    private String apiKey;
    private String accountId;
}

我该如何纠正它?

也许我对此错了,但是

EmailageServiceConfiguration负责创建bean EmailageConfigHolder( @bean注释表示什么(?

那么,弹簧应该如何将其传递给应该创建它的对象的构造函数?这就是您的循环依赖性最有可能源自。

修复它为您的emagageserviceconfiguration创建独立生产商。

相关内容

  • 没有找到相关文章

最新更新