【发布时间】:2019-05-13 13:32:33
【问题描述】:
我试图从控制器外部(在某些线程内)更新表格行数据并总是得到“NullPointerException”。
线程代码:
public class S3Thread implements Runnable {
@Autowired
private IAutomationService automationService;
@Override
public void run() {
Automation config = new Automation("user1","success");
automationService.updateAutomation(config);
}
}
以下行引发了 NullPointer 异常: 自动化服务.updateAutomation(config);
注意:我能够从控制器类创建/更新。仅在线程中。
【问题讨论】:
-
如何调用
run()方法?根据您初始化S3Thread类的方式,@Autowired注释可能无法正常工作。 -
S3Thread 类既不是服务类也不是组件类
-
S3Thread thread1 = new S3Thread();线程 t0 = 新线程(线程 1); t0.start();
-
@Autowired 在这种情况下将不起作用,因为 S3Thread 类是以编程方式实例化的。不受 Spring 管理
-
需要提供 IAutomationService 实现。使用某些工厂或在创建 S3Thread 类的实例时
标签: java spring-boot jpa crud