• springboot由于bean加载顺序导致的问题


    先记录现象:

    dubbo整合zipkin时,我的配置文件是这样的

    @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;
        }

    结果提示自动注入没有发现reporter

    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)}

    然后我改了一下

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

    又提示循环注入:

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

    虽然通过源码发现了导致问题的源码,但是我也不知道具体是什么原因导致的问题

    最后通过修改bean加载的顺序,上述俩个问题都解决了

    @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,放在下面加载是不是就能就觉问题

  • 相关阅读:
    写代码容易,编程并不容易
    微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情-视图渲染...
    微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情-视图渲染...
    微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情-视图渲染...
    微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情-视图渲染...
    Git经典学习指南
    Git经典学习指南
    Git经典学习指南
    Git经典学习指南
    合伙做生意长久的原则
  • 原文地址:https://www.cnblogs.com/jaxlove-it/p/10641359.html
Copyright © 2020-2023  润新知