【问题标题】:autowired object is null in spring TaskExecutor classspring TaskExecutor 类中的自动装配对象为空
【发布时间】:2015-04-19 07:01:36
【问题描述】:

我正在尝试使用 Spring TaskExecutor 使用不同的线程运行方法。我有一个自动装配的依赖,我得到一个 NullPointerException 。自动装配的依赖是空的。请帮忙。

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

【问题讨论】:

  • 如果您自己使用new WebClientTaskExecutor(...) 创建实例,那么spring 将不会进行任何依赖注入。要么让 spring 管理/构建它并从 spring 获取实例,要么自己创建并添加所有依赖项。
  • 如果我理解你的话,我必须自动装配 WebClientTaskExecutor。那么我将如何将 TaskExecutor 对象传递给它??

标签: java spring asynchronous


【解决方案1】:

@Autowired 注释仅适用于作为 Spring bean 的类。因此,您必须将您的类声明为 Spring bean,方法是正确注释 (@Component, @Service, @Repository, @Controller) 或在 Spring XML 上下文文件中定义此 bean。

您也不能自己创建 Spring bean 的实例,而应使用 @Autowired 注释。

【讨论】:

  • 构造函数需要一个TaskExecutor类型的对象。它怎么知道要传递什么对象?
猜你喜欢
  • 2017-08-06
  • 1970-01-01
  • 2017-03-14
  • 2021-02-17
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 1970-01-01
相关资源
最近更新 更多