@Autowired 在 spring 批量自定义编写器中不工作

@Autowired not working in spring batch custom writer

    @Nullable
    @Autowired(required = true)
    CategoryService categoryService;

    @Override
    public void write(List<? extends HashMap<String, Object>> items) throws Exception {
        System.out.println("inside product writer");
        for (HashMap<String, Object> hashMap : items) {
            categoryService.uploadDesktopDataModule(hashMap);
        }
    }

============================================= ===================

我的逻辑是在另一个 class 中将数据存储在 JPARepository 中。 我得到类别服务字段的 null 值。 提前致谢。

这将抛出一个

NullPointerException

在编写器 class 中,当它尝试访问自动连接的服务时 CategoryService,不是因为服务的连接有任何问题,而是因为我实例化了 Writer()手动

Writer writer = new Writer()

@Service@Repository@Controller 都是 @Component 的特化,因此任何要自动连线的都需要用其中之一或 @Component.

并且需要在作业中实例化为@Autowired Writer writer