@Autowired在春季多线程

时间:2019-06-03 18:36:32

标签: spring spring-boot

我有以下课程

@Component
public class CountyScraper implements Runnable {
    volatile private String stateLink;

    @Autowired
    StateScrapeQueueRepository stateScrapeRepository;

    public CountyScraper() {
    }

    public CountyScraper(String stateLink) {
        this.stateLink = stateLink;
    }

    @Override
    public void run() {
            //other code
            stateScrapeRepository.updateScrapeTimestamp(stateLink);    
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
}

我在java.lang.NullPointerException上得到了stateScrapeRepository.updateScrapeTimestamp(stateLink);

这是我创建线程的方式

ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);

for (int i = 0; i < stateLinks.size(); i++) {
    CountyScraper countyScraper = new CountyScraper(stateLinks.get(i));
    executor.execute(countyScraper);
}
executor.shutdown();

我假设在多线程类中使用@Autowired时应该做不同的事情?

0 个答案:

没有答案