Java spring TaskExecutor类中的自动连线对象为空
我正在尝试使用SpringTaskExecutor运行一个使用不同线程的方法。我有一个自动连接的依赖项,我得到一个NullPointerException。自动连接的依赖项为null。请帮忙 TaskExecutor类如下所示:Java spring TaskExecutor类中的自动连线对象为空,java,spring,asynchronous,Java,Spring,Asynchronous,我正在尝试使用SpringTaskExecutor运行一个使用不同线程的方法。我有一个自动连接的依赖项,我得到一个NullPointerException。自动连接的依赖项为null。请帮忙 TaskExecutor类如下所示: public class WebClientTaskExecutor { @Autowired WebClientService webClientService; public class SyncMails implements Runn
public class WebClientTaskExecutor {
@Autowired
WebClientService webClientService;
public class SyncMails implements Runnable {
private String userName;
private Store store;
public SyncMails(String userName, Store store) {
this.userName = userName;
this.store = store;
}
@Override
public void run() {
try {
System.out.println("inside sync mails run method");
String[] folderNames = { "inbox", "sent", "trash", "drafts" };
for (String folderName : folderNames) {
//Null pointer exception in below line.webClientservice=null
int messageCount = WebClientTaskExecutor.this.webClientService.getMessageCount(
folderName, this.store);
int dbLatestMessageNumber = WebClientTaskExecutor.this.webClientService
.getLatestMessageNumberFromDb(folderName,
this.userName, 1);
System.out.println("MEssage count---->" + messageCount);
System.out.println("Latest message count dao--->"
+ dbLatestMessageNumber);
if (messageCount > dbLatestMessageNumber) {
WebClientTaskExecutor.this.webClientService.getMailsFromImap(folderName,
messageCount, dbLatestMessageNumber + 1,
this.store, this.userName);
}
}
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getUserName() {
return userName;
}
public void setUser(String userName) {
this.userName = userName;
}
public Store getStore() {
return store;
}
public void setStore(Store store) {
this.store = store;
}
}
private TaskExecutor taskExecutor;
public WebClientTaskExecutor(TaskExecutor taskExecutor) {
this.taskExecutor = taskExecutor;
}
public void syncMails(String userName,Store store){
System.out.println("web client service object---->" + webClientService);
this.taskExecutor.execute(new SyncMails(userName, store));
}
}
在控制器中,我创建thrTaskExecutor对象并将其交给WebClientTaskExecutor构造函数。然后启动syncmail方法
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(20);
executor.setQueueCapacity(50);
executor.initialize();
WebClientTaskExecutor taskExecutor = new WebClientTaskExecutor(executor);
taskExecutor.syncMails(userName, store);
@Autowired
注释仅在Springbean类中有效。因此,您必须将您的类声明为Springbean,方法是正确地注释它(@Component、@Service、@Repository、@Controller
),或者在SpringXML上下文文件中定义这个bean
您也不能自己创建Springbean的实例,而是使用
@Autowired
注释 如果您自己使用新建WebClientTaskExecutor(…)
创建实例,那么spring将不会执行任何依赖性注入。要么让spring管理/构造它并从spring获取实例,要么自己创建并添加所有依赖项。如果我理解你的意思,我必须自动连接WebClientTaskExecutor。那么我如何将TaskExecutor对象传递给它??构造函数需要TaskExecutor类型的对象。它如何知道要传递的对象?