异常信息
java.lang.NullPointerException: Cannot invoke "top.guli.allutiladmintrailing.service.IAdminService.getAdminById(String)" because "this.iAdminService" is null
at top.guli.allutiladmintrailing.ws.StatusEndpoint.onOpen(StatusEndpoint.java:47) ~[classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.apache.tomcat.websocket.pojo.PojoEndpointBase.doOnOpen(PojoEndpointBase.java:67) ~[tomcat-embed-websocket-10.1.19.jar:10.1.19]
at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:46) ~[tomcat-embed-websocket-10.1.19.jar:10.1.19]
at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:131) ~[tomcat-embed-websocket-10.1.19.jar:10.1.19]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:938) ~[tomcat-embed-core-10.1.19.jar:10.1.19]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1744) ~[tomcat-embed-core-10.1.19.jar:10.1.19]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-10.1.19.jar:10.1.19]
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-10.1.19.jar:10.1.19]
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-10.1.19.jar:10.1.19]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63) ~[tomcat-embed-core-10.1.19.jar:10.1.19]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
我的代码
@ServerEndpoint("/adminStatus/{adminId}")
@Component
public class StatusEndpoint {
private static final Map<String,Session> USER_LIST = new ConcurrentHashMap<>();
private String adminId;
@Autowired
private IAdminService iAdminService;
@OnOpen
public void onOpen(Session session, EndpointConfig config,@PathParam("adminId") String adminId) {
this.adminId = adminId;
USER_LIST.put(this.adminId,session);
AdminEntity adminById = this.iAdminService.getAdminById(this.adminId);
if (!ObjectUtils.isEmpty(adminById)) {
String message = JSON.toJSONString(adminById);
try {
session.getBasicRemote().sendText(message);
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
@OnMessage
public void onMessage(String message) {
System.out.println("收到" + this.adminId + "消息:" + message);
}
@OnClose
public void onClose(Session session) {
USER_LIST.remove(this.adminId);
}
}
我想在这里注入service,然后发送消息给前端,但是我这样注入为null,我不是交给spring管理了吗?为什么还会为null?