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,放在下面加载是不是就能就觉问题
springboot由于bean加载顺序导致的问题的更多相关文章
- SpringBoot系列教程之Bean加载顺序之错误使用姿势辟谣
在网上查询 Bean 的加载顺序时,看到了大量的文章中使用@Order注解的方式来控制 bean 的加载顺序,不知道写这些的博文的同学自己有没有实际的验证过,本文希望通过指出这些错误的使用姿势,让观文 ...
- SpringBoot中的bean加载顺序
https://www.dazhuanlan.com/2019/10/22/5daebc5d16429/ 最近在做传统Spring项目到SpringBoot项目迁移过程中,遇到了一些bean加载顺序的 ...
- springboot 配置文件的加载顺序
springboot 配置文件的加载顺序1.在命令行中传入的参数.2. SPRING APPLICATION JSON中的属性. SPRING_APPLICATION—JSON是以JSON格式配置在系 ...
- 【spring】bean加载顺序
问题来源 有一个bean为A,一个bean为B.想要A在容器实例化的时候的一个属性name赋值为B的一个方法funB的返回值. 如果只是在A里单纯的写着: private B b; private S ...
- SpringBoot的配置文件加载顺序和使用方式
1.bootstrap.properties bootstrap.properties 配置文件是由"根"上下文优先加载,程序启动之初就感知 如:Spring Cloud Conf ...
- spring bean加载顺序指定方式之一
在某些情况下,我们在容器启动的时候做一些事情,举个例子,加载缓存等.. 此时我们会希望某个bean先被加载并执行其中的afterpropertiesset方法. 因为spring默认是通过contex ...
- 记录一个bootstrap因js加载顺序导致的问题(tstrap-table-mobile.min.js:7 Uncaught TypeError: Cannot read property 'defaults' of undefined)
问题描述: 网上找了会没看到答案,然后看了下源码,发现也没有问题,想到js加载的顺序,改了下,发现问题没了. 正确的顺序: 我之前把 <script src="/js/plugins/ ...
- iframe加载顺序导致数据访问出现问题
背景: 一个页面A内有一个iframe,src指向了B页面. 问题: 页面A通过Ajax获取服务器数据,并赋值给了页面A的全局变量gData,页面B要用到页面A的数据gData.那么问题来了当B访问g ...
- SpringBoot的配置文件加载顺序以及如何获取jar包里的资源路径
一.读取配置文件的四种方式 这四种配置文件放置方式的读取优先级依次递减,具体可以查看官方文档. 1.1jar包同级目录下的config文件夹里的配置文件 其实我以前就见过这种方式了,只是不知道怎么做的 ...
随机推荐
- Leetcode题目283.移动零(简单)
题目描述: 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原 ...
- REGIONAL SCRUM GATHERING(RSG)2019 CHINA.
欢迎参加 REGIONAL SCRUM GATHERING(RSG)2019 CHINA. 今年RSG将于2019年8月23号~24号,在北京新世界酒店举办.在为期2天的敏捷大会中,将有接近40位国内 ...
- FreeMarker学习(springmvc配置)
springMvc配置 <bean id="freemarkerConfig" class="org.springframework.web.servlet.vie ...
- Python Docstring 风格和写法学习
什么是Python Docstring 和Java类似,Python也通过注释形式的Docstring给程序.类.函数等建立文档.通过Docstring建立的文档不仅对人来说有更好的可读性,也能够让I ...
- ERS:百科
ylbtech-ERS:百科 ERS=(Enterprises Run System)企业运行系统 企业运行系统-ERS,是继ERP系统之后的企业管理控制软件系统的终极形式,有着非常美好的市场前景,尤 ...
- Swift 条件语句
条件语句通过设定的一个或多个条件来执行程序,在条件为真时执行指定的语句,在条件为 false 时执行另外指定的语句. 可以通过下图来简单了解条件语句的执行过程: Swift 提供了以下几种类型的条件语 ...
- 008-网络抓包工具-wireshark
一.概述 二.安装 三.使用 3.1.基础设置 语言:WireShark→首选项→语言 3.2.使用过程 打开软件,选择对应的网卡, Filter:设置对应的关键词后,点击回车或者右侧箭头 如,协议: ...
- C++ STL nth_element
#include <iostream>#include <algorithm>#include <deque> using namespace std; int m ...
- 图解 HTTP 笔记(四)——HTTP 状态码
本章主要内容是了解 HTTP 状态码的工作机制 状态码的职责是当客户端向服务器端发送请求时,描述返回的请求结果.借助状态码,我们可以了解这次请求是否在服务器端得到了正常的处理. 状态码从其含以上可以分 ...
- 第一章 python+selenium自动化测试实战
@序章 自动化测试是软件测试的主流方向之一: 教程从测试的根本需求出发,讲解如何开展自动化测试. 首先,我们要明白,自动化仅仅是满足我们某种需求的一种工具:没有必要花费时间把它全部弄懂:我们只需要学会 ...