-2
を、私は春の注釈についての問題に会いました。 注釈は@Autowired
です。コードでは@Autowired
がCommandController
クラスの役割を果たしていますが、readVoltageThread
クラスのautowiredの注入が機能しないことがわかりました。readVoltage
メソッドを実行すると、NullPointerExceptionのエラーが発生しました。は:今日@Autowired
@Controller
@RequestMapping("/dongjun")
@SessionAttributes("currentUser")
public class CommandController {
@Autowired
private SimpMessagingTemplate template;
@Autowired
public CommandController(SimpMessagingTemplate template) {
this.template = template;
}
@RequestMapping("/read_voltage")
@ResponseBody
public void readVoltage(@RequestParam(required = true) String switchId,
String type) {
ReadVoltageThread readVoltageThread = new ReadVoltageThread(type, switchId, template);
readVoltageThread.setDaemon(true);
readVoltageThread.start();
}
}
class ReadVoltageThread extends Thread {
@Autowired
private HighVoltageCurrentService currentService2;
@Autowired
private HighVoltageVoltageService voltageService2;
@Autowired
public HighVoltageHitchEventService eventService;
private String type;
private String switchId;
private SimpMessagingTemplate template;
public ReadVoltageThread(String type, String switchId, SimpMessagingTemplate template) {
this.type = type;
this.switchId = switchId;
this.template = template;
}
@Override
public void run() {
while(true) {
try {
sleep(5000);
template.convertAndSend("/topic/read_voltage",
readVoltage(type, switchId));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private Object readVoltage(String type, String switchId) {
Integer[] deStrings = new Integer[3];
switch (type) {
case "0":break;
case "1"://highVoltage
List<HighVoltageVoltage> cliList2 = voltageService2
.getRecentlyVoltage(switchId, "A");
if (cliList2 != null && cliList2.size() != 0) {
deStrings[0] = cliList2.get(0).getValue();
}
cliList2 = voltageService2
.getRecentlyVoltage(switchId, "B");
if (cliList2 != null && cliList2.size() != 0) {
deStrings[1] = cliList2.get(0).getValue();
}
cliList2 = voltageService2
.getRecentlyVoltage(switchId, "C");
if (cliList2 != null && cliList2.size() != 0) {
deStrings[2] = cliList2.get(0).getValue();
}
break;
case "2":break;
default:
break;
}
return deStrings;
}
}
どのように私はそれを修正します。おかげで。