2016-05-04 2 views
-2

Сегодня я встретил проблему с аннотацией весны. Аннотация составляет @Autowired. где в коде @Autowired играет роль в классе CommandController, но я обнаружил, что инъекция автоподпрограммы в классе readVoltageThread не работает, и возникла ошибка исключения NullPointerException при запуске метода readVoltage.Почему два условия имеют разные результаты: @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; 
    } 
} 

Как исправить это. Спасибо.

ответ

2

Когда вы создаете ReadVoltageThread, используя new, Spring не знает об этом, то есть @Autowired, и все остальные аннотации не будут работать.

Вам также нужно автоувеличивать ReadVoltageThread, поэтому он управляется весной, хотя я подозреваю, что в вашем дизайне есть другие проблемы, требующие правильного исправления, вовлекайте намного больше.

Смежные вопросы