java springboot websocket 不能注入( @Autowired ) service bean 报 null 错误

解决方法

spring 或 springboot 的 websocket 里面使用 @Autowired 注入 service 或 bean 时,报空指针异常,service 为 null(并不是不能被注入)。

** 解决方法:将要注入的 service 改成 static,就不会为null了。**
参考代码:

@Controller
@ServerEndpoint(value="/chatSocket")
public class ChatSocket {
    //  这里使用静态,让 service 属于类
    private static ChatService chatService;

    // 注入的时候,给类的 service 注入
    @Autowired
    public void setChatService(ChatService chatService) {
        ChatSocket.chatService = chatService;
    }
}

原因

本质原因:spring管理的都是单例(singleton)和 websocket (多对象)相冲突。
需要了解一个事实:websocket 是多对象的,每个用户的聊天客户端对应 java 后台的一个 websocket 对象,前后台一对一(多对多)实时连接,所以 websocket 不可能像 servlet 一样做成单例的,让所有聊天用户连接到一个 websocket对象,这样无法保存所有用户的实时连接信息。可能 spring 开发者考虑到这个问题,没有让 spring 创建管理 websocket ,而是由 java 原来的机制管理websocket ,所以用户聊天时创建的 websocket 连接对象不是 spring 创建的,spring 也不会为不是他创建的对象进行依赖注入,所以如果不用static关键字,每个 websocket 对象的 service 都是 null。

详细解释(按上面我写的代码,假设属性使用了 static):
初始化:项目启动时,spring 工厂会创建 websocket 的单例对象(此时注解合法,spring 就会为 ChatSocket 类的属性 ChatService 进行注入,并创建一个单例对象,spring 并不知道 websocket 的特殊意义,只是该类的注解合法,便会进行操作,与其他 controller 进行的操作一模一样),因此 chatService 不是 null。

聊天时:当新用户通过客户端聊天时,后台(不管是tomcat 还是java)会根据 ChatSocket 类创建一个新的 chatSocket 对象,保存与用户的连接。因为chatService 是属于类的,所以也不是 null。

总结:
这里 websocket 的多对象机制和 spring 的 controller 注解机制,同时进行,互相没有矛盾。spring 会在初始化时创建一个没有意义的 ChatSocket 的单例对象,该对象在运行期间一直不会被使用,同时为 ChatSocket 的类进行了静态属性的完善,这是 spring 的唯一作用。

当有用户连接聊天时,java 会根据 ChatSocket 类进行创建对象,每个对象保持与对应的用户连接,因为类的静态属性已在启动时被 spring 初始化了,所以每个对象都可以正常使用。

安全性:
安全性要高于单例模式。单例模式全程使用一个对象,而 websocket 使用了多个对象,每个对象互相独立,属性互相分开,唯一的静态属性chatService,只是调用了其方法而已。如果内心实在害怕,自己根据实际情况在 chatService 中使用同步,或者加锁。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要在pom.xml文件中添加Netty和Spring Boot的依赖: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.43.Final</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 然后,创建一个WebSocket客户端类,继承自 `ChannelInboundHandlerAdapter`,实现WebSocket协议相关的方法: ```java @Component public class WebSocketClient extends ChannelInboundHandlerAdapter { private WebSocketClientHandshaker handshaker; private ChannelPromise handshakeFuture; @Autowired private WebSocketClientHandler handler; public void connect(String url) throws Exception { URI uri = new URI(url); String scheme = uri.getScheme() == null ? "ws" : uri.getScheme(); String host = uri.getHost() == null ? "127.0.0.1" : uri.getHost(); int port = uri.getPort(); if (port == -1) { if ("ws".equalsIgnoreCase(scheme)) { port = 80; } else if ("wss".equalsIgnoreCase(scheme)) { port = 443; } } if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) { throw new IllegalArgumentException("Unsupported scheme: " + scheme); } final WebSocketClientHandler handler = this.handler; EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .option(ChannelOption.SO_KEEPALIVE, true) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if ("wss".equalsIgnoreCase(scheme)) { SslContext sslContext = SslContextBuilder.forClient().build(); pipeline.addLast(sslContext.newHandler(ch.alloc(), host, port)); } pipeline.addLast(new HttpClientCodec(), new HttpObjectAggregator(8192), WebSocketClientCompressionHandler.INSTANCE, handler); } }); Channel channel = bootstrap.connect(uri.getHost(), port).sync().channel(); handshaker = WebSocketClientHandshakerFactory.newHandshaker( uri, WebSocketVersion.V13, null, true, new DefaultHttpHeaders()); handshakeFuture = channel.newPromise(); handler.setHandshakeFuture(handshakeFuture); channel.writeAndFlush(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, uri.getRawPath())) .addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { handshaker.handshake(future.channel()); } else { handshakeFuture.setFailure(future.cause()); } } }); handshakeFuture.sync(); channel.closeFuture().sync(); } finally { group.shutdownGracefully(); } } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { handler.setCtx(ctx); handshaker.handshake(ctx.channel()); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println("WebSocket Client disconnected!"); handler.setCtx(null); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { try { handshaker.finishHandshake(ch, (FullHttpResponse) msg); System.out.println("WebSocket Client connected!"); handshakeFuture.setSuccess(); } catch (WebSocketHandshakeException e) { System.out.println("WebSocket Client failed to connect"); handshakeFuture.setFailure(e); } return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException( "Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; System.out.println("WebSocket Client received message: " + textFrame.text()); } else if (frame instanceof PongWebSocketFrame) { System.out.println("WebSocket Client received pong"); } else if (frame instanceof CloseWebSocketFrame) { System.out.println("WebSocket Client received closing"); ch.close(); } } } ``` 在这个类中,我们注入了一个 `WebSocketClientHandler` 的实例,它也是一个 `ChannelInboundHandlerAdapter`,用于处理WebSocket消息。在 `connect()` 方法中,我们使用Netty创建了一个Bootstrap实例,并设置了一些参数,然后连接到WebSocket服务器。在连接成功后,进行了握手操作,以确保连接正常建立。 下面是 `WebSocketClientHandler` 的实现: ```java @Component public class WebSocketClientHandler extends ChannelInboundHandlerAdapter { private ChannelHandlerContext ctx; private ChannelPromise handshakeFuture; public void setCtx(ChannelHandlerContext ctx) { this.ctx = ctx; } public void setHandshakeFuture(ChannelPromise handshakeFuture) { this.handshakeFuture = handshakeFuture; } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt == WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE) { handshakeFuture.setSuccess(); } else { super.userEventTriggered(ctx, evt); } } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { // Handle WebSocket message } // Other methods } ``` 在这个类中,我们重写了 `channelRead()` 方法,用于处理WebSocket消息。我们也保存了当前的 `ChannelHandlerContext` 和握手操作的 `ChannelPromise`,以便在连接成功后进行操作。 最后,在Spring Boot的主类中,可以通过注入 `WebSocketClient` 的方式来启动WebSocket客户端: ```java @SpringBootApplication public class Application { @Autowired private WebSocketClient webSocketClient; public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } @EventListener(ApplicationReadyEvent.class) public void connect() throws Exception { webSocketClient.connect("ws://localhost:8080/websocket"); } } ``` 在 `connect()` 方法中,我们调用了 `WebSocketClient` 的 `connect()` 方法,来连接到WebSocket服务器。注意,这个方法是阻塞的,直到连接关闭或连接失败。 这样,我们就成功地使用Netty和Spring Boot实现了一个WebSocket客户端,并且可以将其注入Spring Boot的Bean容器中,以方便管理和使用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值