[英]Spring Service returns null
我創建了一個包含對象列表的服務,並希望在其他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 {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.