tryingHard
tryingHard

Reputation: 2084

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:

@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? I don't have any xml file that i could use.

As i read here: what are the different ways of creating beanfactory object? We can create this BeanFactory by:

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? I just want that my code was compatible with the older version (SpringBoot 1.5 version).

Upvotes: 0

Views: 1024

Answers (1)

Marcin Grzejszczak
Marcin Grzejszczak

Reputation: 11169

Spring creates BeanFactory for you. If you're using a Spring context, you get it out of the box. 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. BeanFactory is an interface so what you can do is to override the getBean method to retrieve such beans as:

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)

But just by using Spring you get everything out of the box. Quite frankly, I don't really understand what you're doing.

Upvotes: 1

Related Questions