Spring Service返回null

时间:2019-02-14 10:25:30

标签: java spring

我创建了一个包含对象列表的服务,并且希望在其他2个组件中进行设置。一个填充服务中的列表,另一个用于通过列表中的Websocket发送消息(WebSocketConnection是一个具有Session和TextMessage的对象,用于与客户端建立连接。发送消息按预期工作,我已经测试过多个客户端正在发送消息。

WebSocketSessionService:

public class WebSocketSessionService {

private List<WebSocketConnection> sessions;

public WebSocketSessionService()
{
    sessions = new ArrayList<WebSocketConnection>();
}

public void addNewSession(WebSocketConnection newSession)
{
    sessions.add(newSession);
}

public List<WebSocketConnection> getAllSessions()
{
    return sessions;
}

public WebSocketConnection getSessionInList(WebSocketConnection session)
{
    System.out.println("alsdsada");
    for (WebSocketConnection webSocketConnection : sessions) {
        System.out.println("alsdsada");
        System.out.println(session.getSession().getId());
        System.out.println(webSocketConnection.getSession().getId());
        if(session.getSession().getId().equals(webSocketConnection.getSession().getId()))
        {
            return webSocketConnection;
        }
    }
    WebSocketConnection webSocketConnection = new WebSocketConnection();
    System.out.println(webSocketConnection.isDummy());
    return new WebSocketConnection();
}

public void printAllSessionId()
{
    for (WebSocketConnection webSocketConnection : sessions) {

        System.out.println(webSocketConnection.getSession().getId());
    }
}
}

WebSocketHandler:

 public class WebSocketHandler extends AbstractWebSocketHandler {

@Autowired
public WebSocketSessionService webSocketSessionsService; //HERE

public WebSocketHandler()
{
    this.webSocketSessionsService= new WebSocketSessionService();
}

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {    

WebSocketConnection newDataRecieved = new WebSocketConnection(session,message);
System.out.println("Created New WebSocketConnection");
if( ! webSocketSessionsService.getSessionInList(newDataRecieved).isDummy())
{
    webSocketSessionsService.getSessionInList(newDataRecieved).addNewMessage(message);
    System.out.println("Added New Message");
}
else
{
    webSocketSessionsService.addNewSession(newDataRecieved);
    System.out.println("Added New Session");
}
webSocketSessionsService.printAllSessionId();




}

@Override
protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws IOException {

}

}

HomeController:

@Controller
@ComponentScan
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);

@Autowired
private VehicleService vehicleService;
@Autowired
private GlobalController globalController;

@Autowired
private WebSocketSessionService webSocketSessionsService; //HERE

@RequestMapping(value = {"/vehicle/saveVehicle"}, method = RequestMethod.POST)
public String saveTodo(@ModelAttribute("reqVehicle") Vehicle reqVehicle,
                       final RedirectAttributes redirectAttributes) {
    logger.info("/vehicle/save");
    try {
        reqVehicle.setCreateDate(LocalDateTime.now());
        reqVehicle.setStatus(Status.ACTIVE.getValue());
        reqVehicle.setUserId(globalController.getLoginUser().getId());
        vehicleService.save(reqVehicle);
        redirectAttributes.addFlashAttribute("msg", "success");

        //SEND MSG TO CLIENT THRU SOCKET
        System.out.println("ALL SESSIONS");
        webSocketSessionsService.printAllSessionId(); //NULL POINTER <--


        webSocketSessionsService.getAllSessions().get(0).sendNewTextMessage("Git Good!");


    } catch (Exception e) {
        redirectAttributes.addFlashAttribute("msg", "fail");
        logger.error("save: " + e.getMessage());
    }

    return "redirect:/home";
}
}

我希望在WebSocketHandler中填充并在HomeController中使用。

1 个答案:

答案 0 :(得分:0)

this.webSocketSessionsService= new WebSocketSessionService();的构造方法中删除WebSocketHandler

然后将您的WebSocketSessionService设为Spring托管的bean,因此将@Service添加到类中

@Service
public class WebSocketSessionService {

}