簡體   English   中英

Spring TaskExecutor類中的自動裝配對象為null

[英]autowired object is null in spring TaskExecutor class

我正在嘗試使用Spring TaskExecutor使用不同的線程運行方法。 我有一個自動裝配的依存關系,我得到了NullPointerException。自動裝配的依賴為null。 請幫忙。

TaskExecutor類如下:

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構造函數。然后啟動syncMails方法。

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注釋僅在Spring Bean類中有效。 因此,必須通過正確注釋@Component, @Service, @Repository, @Controller )或通過在Spring XML上下文文件中定義此bean來將類聲明為Spring bean。

您也不能自己創建Spring bean的實例,而應使用@Autowired批注。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM