我已获得该服务,并且已使用@Service
注释了它的实现。它适用于单线程应用程序,但现在我应该使用此服务从另一个线程做一些逻辑。这是我的代码:
import org.springframework.beans.factory.annotation.Autowired;
import ru.tenet.smsc.form.MassSMSForm;
import ru.tenet.smsc.service.SaveBatchService;
public class MassSmsRunnable implements Runnable {
private MassSMSForm massSMSForm;
private String userName;
@Autowired
SaveBatchService saveBatchService;
public MassSmsRunnable(MassSMSForm massSMSForm, String userName) {
this.massSMSForm = massSMSForm;
this.userName = userName;
}
@Override
public void run() {
saveBatchService.sendMass(massSMSForm, userName); //here it is
saveBatchService.save();
}
}
但NullPointerException
就是我所拥有的一切:
Exception in thread "Thread-3" java.lang.NullPointerException
at ru.tenet.smsc.thread.MassSmsRunnable.run(MassSmsRunnable.java:31)
at java.lang.Thread.run(Thread.java:744)
那么,我该如何解决这个问题?
答案 0 :(得分:0)
@Component
public class MassSmsRunnable implements Runnable
无论你使用的是runnable
@Inject
private MassSmsRunnable massSmsRunnable;
在MassSmsRunnable中,你需要以类似的方式注入massSMSForm和用户名。