更早的这段代码可以正常工作:
@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);
}
}
但是现在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);
}
}
我遇到的问题与BeanFactory
有关,我不知道如何创建它?
我没有可以使用的xml
文件。
我在这里阅读:what are the different ways of creating beanfactory object?
我们可以通过以下方式创建此BeanFactory
:
1. BeanFactory fac=new ClassPathXmlApplicationContext("Spring-Config.xml");
2. Resource res=new Classpathresource("Spring-Config.xml");
BeanFactory fac=new XmlBeanFactory(res);
如果我没有任何xml
文件,如何简单地创建它?我只希望我的代码与旧版本(SpringBoot 1.5版本)兼容。
答案 0 :(得分:1)
Spring为您创建BeanFactory
。如果您使用的是Spring上下文,则可以直接使用它。但是,如果由于某种原因而不是使用Spring但使用了Sleuth(我不知道为什么要这么做),则可以检查我们是否使用BeanFactory
来检索bean。 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;
}
您需要模拟BeanFactory
以返回this.beanFactory.getBean(Tracing.class)
并
this.beanFactory.getBean(SpanNamer.class)
但是,仅通过使用Spring,便可以立即使用所有内容。坦率地说,我不太了解您在做什么。