简体   繁体   English

Cloud Sleuth从SpringBoot 1.5更改为2.x

[英]Cloud Sleuth change from SpringBoot 1.5 to 2.x

Earlier this code worked fine: 之前的代码可以正常工作:

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
class CustomTraceableExecutorServiceImpl implements ExecutorServiceProvider {

    @Qualifier(value = "defaultExecutorService")
    private final ExecutorService executorService;
    @Qualifier(value = "scheduledTimeoutExecutorService")
    private final ScheduledExecutorService scheduledExecutorService;
    private final Tracer tracer;
    private final SpanNamer spanNamer;

    @Override
    public ExecutorService get() {
        return new TraceableExecutorService(executorService, tracer, new TraceKeys(), spanNamer);
    }

    @Override
    public ScheduledExecutorService getScheduled() {
        return new TraceableScheduledExecutorService(scheduledExecutorService, tracer, new TraceKeys(), spanNamer);
    }
}

But now TraceableExecutorService constructor takes TraceableExecutorService(BeanFactory beanFactory, final ExecutorService delegate) so i need to change code to sth like: 但是现在TraceableExecutorService构造函数采用TraceableExecutorService(BeanFactory beanFactory, final ExecutorService delegate)所以我需要将代码更改为:

@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
class CustomTraceableExecutorServiceImpl implements ExecutorServiceProvider {

    @Qualifier(value = "defaultExecutorService")
    private final ExecutorService executorService;
    @Qualifier(value = "scheduledTimeoutExecutorService")
    private final ScheduledExecutorService scheduledExecutorService;
    //@Qualifier(value = "defaultBeanFactory")
    @Autowired
    private final BeanFactory beanFactory;
    //@Qualifier(value = "scheduledBeanFactory")
    @Autowired
    private final BeanFactory scheduledBeanFactory;

    @Override
    public ExecutorService get() {
        return new TraceableExecutorService(beanFactory, executorService);
    }

    @Override
    public ScheduledExecutorService getScheduled() {
        return new TraceableScheduledExecutorService(scheduledBeanFactory, scheduledExecutorService);
    }
}

The issue i have is about BeanFactory i don't know how to create it? 我遇到的问题与BeanFactory有关,我不知道如何创建它? I don't have any xml file that i could use. 我没有可以使用的任何xml文件。

As i read here: what are the different ways of creating beanfactory object? 正如我在这里所读: 创建beanfactory对象的不同方式是什么? We can create this BeanFactory by: 我们可以通过以下方式创建此BeanFactory

1. BeanFactory fac=new ClassPathXmlApplicationContext("Spring-Config.xml"); 

2. Resource res=new Classpathresource("Spring-Config.xml");
    BeanFactory fac=new XmlBeanFactory(res);

How can i create it simply if i don't have any xml file? 如果我没有任何xml文件,该如何简单地创建它? I just want that my code was compatible with the older version (SpringBoot 1.5 version). 我只希望我的代码与旧版本(SpringBoot 1.5版本)兼容。

Spring creates BeanFactory for you. Spring为您创建BeanFactory If you're using a Spring context, you get it out of the box. 如果您使用的是Spring上下文,则可以直接使用它。 If, however, for some reason you're not using Spring but you use Sleuth (I have no idea why would you do that), you can check that we use BeanFactory to retrieve beans. 但是,如果由于某种原因而不是使用Spring但使用了Sleuth(我不知道为什么要这么做),则可以检查是否使用BeanFactory来检索bean。 BeanFactory is an interface so what you can do is to override the getBean method to retrieve such beans as: BeanFactory是一个接口,因此您可以做的是重写getBean方法以检索诸如以下这样的bean:

Tracing tracing() {
        if (this.tracing == null && this.beanFactory != null) {
            this.tracing = this.beanFactory.getBean(Tracing.class);
        }
        return this.tracing;
    }

    SpanNamer spanNamer() {
        if (this.spanNamer == null && this.beanFactory != null) {
            this.spanNamer = this.beanFactory.getBean(SpanNamer.class);
        }
        return this.spanNamer;
    }

You need to mock the BeanFactory to return this.beanFactory.getBean(Tracing.class) and this.beanFactory.getBean(SpanNamer.class) 您需要模拟BeanFactory以返回this.beanFactory.getBean(Tracing.class)this.beanFactory.getBean(SpanNamer.class)

But just by using Spring you get everything out of the box. 但是,仅通过使用Spring,便可以立即使用所有功能。 Quite frankly, I don't really understand what you're doing. 坦率地说,我不太了解您在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM