当前位置: 代码迷 >> java >> Spring Service返回null
  详细解决方案

Spring Service返回null

热度:63   发布时间:2023-08-02 10:25:35.0

我创建了一个包含对象列表的服务,并希望在其他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中使用。

删除this.webSocketSessionsService= new WebSocketSessionService(); 来自WebSocketHandler构造方法。

然后将您的WebSocketSessionService变成一个Spring托管的bean,因此将@Service添加到该类中

@Service
public class WebSocketSessionService {

}
  相关解决方案