springboot因爲bean加載順序致使的問題

先記錄現象:spring

dubbo整合zipkin時,個人配置文件是這樣的api

@Bean("okHttpSender")
    public OkHttpSenderFactoryBean okHttpSender(){
        OkHttpSenderFactoryBean okHttpSenderFactoryBean = new OkHttpSenderFactoryBean();
        //zipkin服務端
        okHttpSenderFactoryBean.setEndpoint("http://localhost:9411/api/v2/spans");
        return okHttpSenderFactoryBean;
    }

    @Bean
    public AsyncReporterFactoryBean reporter(@Qualifier("okHttpSender")OkHttpSender sender){
        AsyncReporterFactoryBean asyncReporterFactoryBean = new AsyncReporterFactoryBean();
        asyncReporterFactoryBean.setSender(sender);
        asyncReporterFactoryBean.setCloseTimeout(1000);
        return asyncReporterFactoryBean;
    }

    @Bean("tracing")
    public TracingFactoryBean tracingFactoryBean(@Qualifier("reporter") Reporter reporter) throws Exception {
        TracingFactoryBean tracingFactoryBean = new TracingFactoryBean();
        tracingFactoryBean.setLocalServiceName("rcinvestTracing");
        tracingFactoryBean.setSpanReporter(reporter);
        CurrentTraceContextFactoryBean currentTraceContextFactoryBean = new CurrentTraceContextFactoryBean();
        CurrentTraceContext.ScopeDecorator scopeDecorator = MDCScopeDecorator.create();
        currentTraceContextFactoryBean.setScopeDecorators(Arrays.asList(scopeDecorator));
        tracingFactoryBean.setCurrentTraceContext(currentTraceContextFactoryBean.getObject());
        return tracingFactoryBean;
    }

結果提示自動注入沒有發現reporterasync

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'zipkin2.reporter.Reporter<?>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=reporter)}

而後我改了一下spa

 public TracingFactoryBean tracingFactoryBean(@Qualifier("reporter") Reporter reporter) throws Exception
改成:
 public TracingFactoryBean tracingFactoryBean(@Qualifier("reporter") AsyncReporterFactoryBean reporter) throws Exception

又提示循環注入:code

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

雖然經過源碼發現了致使問題的源碼,可是我也不知道具體是什麼緣由致使的問題blog

最後經過修改bean加載的順序,上述倆個問題都解決了ip

@Bean("tracing")
    public TracingFactoryBean tracingFactoryBean(@Qualifier("reporter") AsyncReporter reporter) throws Exception {
        TracingFactoryBean tracingFactoryBean = new TracingFactoryBean();
        tracingFactoryBean.setLocalServiceName("rcinvestTracing");
        tracingFactoryBean.setSpanReporter(reporter);
        CurrentTraceContextFactoryBean currentTraceContextFactoryBean = new CurrentTraceContextFactoryBean();
        CurrentTraceContext.ScopeDecorator scopeDecorator = MDCScopeDecorator.create();
        currentTraceContextFactoryBean.setScopeDecorators(Arrays.asList(scopeDecorator));
        tracingFactoryBean.setCurrentTraceContext(currentTraceContextFactoryBean.getObject());
        return tracingFactoryBean;
    }

    @Bean
    public AsyncReporterFactoryBean reporter(@Qualifier("okHttpSender")OkHttpSender sender){
        AsyncReporterFactoryBean asyncReporterFactoryBean = new AsyncReporterFactoryBean();
        asyncReporterFactoryBean.setSender(sender);
        asyncReporterFactoryBean.setCloseTimeout(1000);
        return asyncReporterFactoryBean;
    }

    @Bean("okHttpSender")
    public OkHttpSenderFactoryBean okHttpSender(){
        OkHttpSenderFactoryBean okHttpSenderFactoryBean = new OkHttpSenderFactoryBean();
        //zipkin服務端
        okHttpSenderFactoryBean.setEndpoint("http://localhost:9411/api/v2/spans");
        return okHttpSenderFactoryBean;
    }

猜想:父級bean放在上面加載,須要注入的bean,放在下面加載是否是就能就覺問題ci

相關文章
相關標籤/搜索