如何自动装配Spring TaskExecutor创建的线程? [英] How do I autowire a Spring TaskExecutor created thread?
问题描述
根据 Spring的文档使用TaskExecutor的方法如下:
According to Spring's documentation the way to use the TaskExecutor is as follows:
import org.springframework.core.task.TaskExecutor;
public class TaskExecutorExample {
private class MessagePrinterTask implements Runnable {
private String message;
public MessagePrinterTask(String message) {
this.message = message;
}
public void run() {
System.out.println(message);
}
}
private TaskExecutor taskExecutor;
public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask("Message" + i));
}
}
}
但是,如果MessagePrinterTask已自动装配它们不会被Spring配置它们的依赖关系,因为我们在Spring的上下文之外实例化我们的bean(至少我就是这样理解它),即使Spring会提供实际的线程创建。 如果MessagePrinterTask具有自动连接的依赖关系,我们如何让Spring识别它们?我尝试了以下修改示例无效(是的,正确启用了自动装配):
However, if MessagePrinterTask has autowired dependencies they will not be configured by Spring because we are instantiating our bean outside of Spring's context (at least that's how I understand it) even though Spring will provide the actual thread creation. If MessagePrinterTask were to have autowired dependencies how do we get Spring to recognize them? I tried the following modified example to no avail (and yes, autowiring is enabled properly):
import org.springframework.core.task.TaskExecutor;
public class TaskExecutorExample {
@Component
private class MessagePrinterTask implements Runnable {
@Autowired
private autoWiredDependency;
public void run() {
autoWiredDependency.doNotThrowNullPointerExceptionPlease();
}
}
private TaskExecutor taskExecutor;
public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask());
}
}
}
推荐答案
我认为有两种方法可以解决这个问题:
There are two ways I think that you can go about this:
a。提供任务的依赖关系 - 这样:
a. Provide the dependencies to the Task - this way:
class MessagePrinterTask implements Runnable {
public MessagePrinterTask(ADependency aDependency){
this.aDependency = aDependency;
}
private ADependency aDependency;
public void run() {
aDependency.doNotThrowNullPointerExceptionPlease();
}
}
在你的TaskExectorExample中可以是单身:
And in your TaskExectorExample which can be the singleton:
import org.springframework.core.task.TaskExecutor;
public class TaskExecutorExample {
@Autowired private ADependency aDependency;
@Autowired
public TaskExecutorExample(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void printMessages() {
for(int i = 0; i < 25; i++) {
taskExecutor.execute(new MessagePrinterTask(this.aDependency));
}
}
}
b。在MesasgePrinterTask上使用@Configurable注释,这将依赖注入到MessagePrinterTask中,即使它是在Spring容器之外实例化的 - 虽然使用@Configurable(需要AspectJ)有一些捕获:
b. Using @Configurable annotation on your MesasgePrinterTask, this will inject in dependencies into MessagePrinterTask even though it is instantiated outside of a Spring Container - there are some catches in using @Configurable though(requires AspectJ):
@Configurable
class MessagePrinterTask implements Runnable {
这篇关于如何自动装配Spring TaskExecutor创建的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!