欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Springboot使用Websocket的時(shí)候調(diào)取IOC管理的Bean報(bào)空指針異常問題

 更新時(shí)間:2025年05月15日 10:03:02   作者:墮落年代  
這篇文章主要介紹了Springboot使用Websocket的時(shí)候調(diào)取IOC管理的Bean報(bào)空指針異常問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

問題

這個(gè)問題主要是因?yàn)閃ebsocket的工作方式導(dǎo)致的,下面是詳細(xì)解決方案

解決

WebSocket 端點(diǎn)類通常不受 Spring IOC 管理的原因在于它們是由 WebSocket 容器(例如,Tomcat、Jetty 等)而不是 Spring 容器管理的。

WebSocket 規(guī)范(JSR 356)定義了 WebSocket 端點(diǎn)的生命周期和管理方式,這通常與 Spring 的生命周期和依賴注入機(jī)制不同。

以下是一些具體原因和解決方法:

原因

不同的生命周期管理

  • WebSocket 端點(diǎn)的創(chuàng)建和管理是由 Web 容器(如 Tomcat、Jetty 等)負(fù)責(zé)的,而不是由 Spring 容器負(fù)責(zé)。
  • 這意味著 WebSocket 端點(diǎn)類的實(shí)例化和生命周期管理不在 Spring 的控制范圍內(nèi)。

注入機(jī)制不同

  • Spring 的依賴注入機(jī)制依賴于 Spring 容器管理的 Bean,但 WebSocket 端點(diǎn)類實(shí)例化時(shí),Web 容器并不知道如何進(jìn)行依賴注入。

解決方法

方法一:使用 Spring Boot 和 @Configuration 配置

在 Spring Boot 項(xiàng)目中,可以通過配置類和自定義 WebSocket 處理器來管理 WebSocket 連接,這樣可以使用 Spring 容器管理的 Bean。

創(chuàng)建 WebSocket 配置類

import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final MyWebSocketHandler myWebSocketHandler;

    public WebSocketConfig(MyWebSocketHandler myWebSocketHandler) {
        this.myWebSocketHandler = myWebSocketHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(myWebSocketHandler, "/websocket").setAllowedOrigins("*");
    }
}

創(chuàng)建 WebSocket 處理器類

import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.stereotype.Component;

@Component
public class MyWebSocketHandler extends TextWebSocketHandler {

    private final SomeService someService;

    public MyWebSocketHandler(SomeService someService) {
        this.someService = someService;
    }

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        String payload = message.getPayload();
        System.out.println("消息為:" + payload);
        someService.doSomething();  // 使用 Spring Bean
        session.sendMessage(new TextMessage("servet 發(fā)送:" + payload));
    }
}

方法二:使用自定義 SpringConfigurator

創(chuàng)建自定義的 SpringConfigurator

import javax.websocket.server.ServerEndpointConfig;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

public class SpringConfigurator extends ServerEndpointConfig.Configurator {

    @Override
    public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
        WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
        if (context == null) {
            throw new InstantiationException("Unable to get Spring context.");
        }
        return context.getAutowireCapableBeanFactory().createBean(clazz);
    }
}

@ServerEndpoint 注解中指定 configurator

import javax.websocket.OnMessage;
import javax.websocket.server.ServerEndpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@ServerEndpoint(value = "/websocket", configurator = SpringConfigurator.class)
@Component
public class MyWebSocket {

    @Autowired
    private SomeService someService;  // 由 Spring 管理的 Bean

    @OnMessage
    public String onMsg(String text) throws IOException {
        System.out.println("消息為:" + text);
        someService.doSomething();  // 使用 Spring Bean
        return "servet 發(fā)送:" + text;
    }
}

方法三:手動(dòng)獲取 Spring Bean

創(chuàng)建 ApplicationContextProvider 類

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        context = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return context;
    }
}

在 WebSocket 類中使用 ApplicationContextProvider 獲取 Bean

import javax.websocket.OnMessage;
import javax.websocket.server.ServerEndpoint;
import org.springframework.stereotype.Component;

@ServerEndpoint(value = "/websocket")
@Component
public class MyWebSocket {

    private SomeService someService;

    public MyWebSocket() {
        this.someService = ApplicationContextProvider.getApplicationContext().getBean(SomeService.class);
    }

    @OnMessage
    public String onMsg(String text) throws IOException {
        System.out.println("消息為:" + text);
        someService.doSomething();  // 使用 Spring Bean
        return "servet 發(fā)送:" + text;
    }
}

通過這些方法,你可以確保 WebSocket 端點(diǎn)類能夠正確地訪問由 Spring IOC 管理的 Bean,從而避免空指針異常。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢

    使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢

    這篇文章主要介紹了使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 詳解Java編程中對線程的中斷處理

    詳解Java編程中對線程的中斷處理

    這篇文章主要介紹了Java編程中對線程的中斷處理,特別講解了中斷的時(shí)機(jī)與中斷狀態(tài)的管理,需要的朋友可以參考下
    2015-11-11
  • SpringBoot分布式WebSocket的實(shí)現(xiàn)指南

    SpringBoot分布式WebSocket的實(shí)現(xiàn)指南

    在現(xiàn)代Web應(yīng)用中,實(shí)時(shí)通信已成為基本需求,而WebSocket是實(shí)現(xiàn)這一功能的核心技術(shù),本文將詳細(xì)介紹如何在Spring Boot項(xiàng)目中實(shí)現(xiàn)分布式WebSocket,包括完整的技術(shù)方案、實(shí)現(xiàn)步驟和核心代碼,需要的朋友可以參考下
    2025-10-10
  • 聊聊如何打印GC日志排查的問題

    聊聊如何打印GC日志排查的問題

    這篇文章主要介紹了聊聊如何打印GC日志排查的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java中Map、Set、List的簡單使用教程(快速入門)

    java中Map、Set、List的簡單使用教程(快速入門)

    這篇文章主要給大家介紹了關(guān)于java中Map、Set、List簡單使用教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Spring?Data?Jpa?中原生查詢?REGEXP?的使用詳解

    Spring?Data?Jpa?中原生查詢?REGEXP?的使用詳解

    這篇文章主要介紹了Spring?Data?Jpa?中原生查詢?REGEXP?的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 親測解決,nacos下線失敗問題

    親測解決,nacos下線失敗問題

    這篇文章主要介紹了親測解決,nacos下線失敗問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • maven打包加入本地jar包的實(shí)現(xiàn)

    maven打包加入本地jar包的實(shí)現(xiàn)

    在使用maven打包的過程中,有時(shí)候我們需要添加一些本地的jar包,本文主要介紹了maven打包加入本地jar包的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • Log4j關(guān)閉Spring和Hibernate日志打印方式

    Log4j關(guān)閉Spring和Hibernate日志打印方式

    這篇文章主要介紹了Log4j關(guān)閉Spring和Hibernate日志打印方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java異常javax.net.ssl.SSLHandshakeException: SSL的解決方法

    Java異常javax.net.ssl.SSLHandshakeException: SSL的解決方法

    在Java開發(fā)過程中,SSL(Secure Sockets Layer)握手異常是一個(gè)常見的網(wǎng)絡(luò)通信錯(cuò)誤,特別是在使用HTTPS協(xié)議進(jìn)行安全通信時(shí),本文將詳細(xì)分析javax.net.ssl.SSLHandshakeException: SSL這一異常的背景、可能的原因,并通過代碼示例幫助您理解和解決這一問題
    2024-12-12

最新評論