我有一个@Component类:
@Component
@ServerEndpoint("/ws")
public class WebSocketHandler {
@Autowired
private UserService userService;
@OnOpen
public void onOpen(Session session) throws IOException, EncodeException {
List<User> users = userService.findAllByOnlineIsTrue();
session.getBasicRemote().sendObject(new RestResponse(Status.SUCCESSFUL, "online users", users));
System.out.println("Open WebSocket connection...");
}
}
UserService
是一个接口,它具有一个实现:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private MongoTemplate template;
//...
}
调用onOpen
的{{1}}方法时,由于WebSocketHandler
为空,因此抛出了NullPointerException
。这很奇怪,因为我也有一个控制器类,并且userService
很好:
userService
我不明白。有人可以帮忙吗?