0

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

tryingHard
  • 1,794
  • 4
  • 35
  • 74

1 Answers1

1

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.

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • `TraceableExecutorService` (in newer version) constructor changed and now it accepts `BeanFactory and ExecutorService` not `tracer` or `spanNamer` as you mentioned about them and i do not know why? And yes - i am using spring. I am trying to write code so it would work on this newer version of constructor and be `logically` the same as in previous older constructor version. I think that there are logically different (the newer version wrote by me is probably wrong). – tryingHard Oct 23 '18 at 08:55
  • Just pass the bean factory and that's it. There's nothing else you have to do – Marcin Grzejszczak Oct 23 '18 at 08:58
  • Also please read the migration guide - https://github.com/spring-cloud/spring-cloud-sleuth/wiki/Spring-Cloud-Sleuth-2.0-Migration-Guide – Marcin Grzejszczak Oct 23 '18 at 08:59
  • So the code (second version) I wrote above is fine or not? Is it logically the same as the first code i posted in the question? As you can see i passed the `bean factory`. – tryingHard Oct 23 '18 at 09:06
  • Marcin - jak Polak do Polaka - udało mi się dobrze zmienić kod na nowszą wersję, czy coś jest nie tak? Pierwszy blok kodu to stara wersja, drugi nowa i chcę żeby logika była zachowana. Stronę co podałeś widziałem - stąd zmiana w kodzie taka, a nie inna. – tryingHard Oct 23 '18 at 09:19
  • 1
    Let's switch back to English ;) Just inject, via constructor, bean of type `BeanFactory`. That will work like the previous version. – Marcin Grzejszczak Oct 23 '18 at 09:28
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/182340/discussion-between-yami-and-marcin-grzejszczak). – tryingHard Oct 23 '18 at 09:42