直接注入报空指针异常
@ServerEndpoint(value = "/websocket/{pageCode}",encoders = {ServerEncoder.class})
@Component
public class WebSocketServer {
@Resource
UserLoginService userLoginService;
手动获取Bean->工具类
@ServerEndpoint(value = "/websocket/{pageCode}",encoders = {ServerEncoder.class})
@Component
public class WebSocketServer {
@Resource
UserLoginService userLoginService;
private static ApplicationContext applicationContext;
public static void setApplicationContext(ApplicationContext context) {
applicationContext = context;
}
.....
}
手动获取Bean->启动类
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(xxx.class, args);
WebSocketServer.setApplicationContext(applicationContext);
}