SpringBoot自定义类@Autowire注入为null的问题
在使用websocket实现即时聊天时,遇到了service注入为空,经过多种尝试,最终解决了这个bug
@Component
public class ChatWebSocketServerEndpoint {
private static final Logger log = LoggerFactory.getLogger(ChatWebSocketServerEndpoint.class);
private static ChatWebSocketServerEndpoint chatWebSocketServerEndpoint;
@Autowired
private WebSocketService webSocketService;
@PostConstruct
public void init(){
chatWebSocketServerEndpoint = this;
chatWebSocketServerEndpoint.webSocketService = this.webSocketService;
}
}
//使用方法
MsgDTO dto = chatWebSocketServerEndpoint.webSocketService.saveChatMsg(chatMsg);