netty无法导入注入Bean问题解决

这个问题和weboscket一样
* 因 SpringBoot WebSocket 对每个客户端连接都会创建一个 WebSocketServer(@ServerEndpoint 注解对应的) 对象,Bean 注入操作会被直接略过,因而手动注入一个全局变量,spring管理的都是单对象,和Websocket 多对象相冲突。
同理 netty也是多对象,spring无法管理

解决方法:

  1. 网上出现较多的主动通过getBean的方式来获取
  2. 上面的过于麻烦,尝试使用解决weboscket无法注入的方法来解决,成功。
    参考 https://blog.csdn.net/m0_37202351/article/details/86255132

@Component
@Slf4j
public class ServerHandler extends ChannelInboundHandlerAdapter {

public static RecordService recordServiceImpl;
@Autowired
public void setRecordServiceImpl(RecordService recordServiceImpl) {
    ServerHandler.recordServiceImpl = recordServiceImpl;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在Spring Boot中整合Netty,可以使用Spring的依赖注入来管理Netty的组件,这样可以更方便地管理Netty的资源。具体步骤如下: 1. 在pom.xml中添加netty和spring-boot-starter-web依赖: ```xml <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.51.Final</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2. 创建Netty的ChannelInitializer,并使用Spring注入需要的对象: ```java @Component public class NettyServerInitializer extends ChannelInitializer<SocketChannel> { @Autowired private NettyServerHandler nettyServerHandler; @Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); pipeline.addLast(nettyServerHandler); } } ``` 3. 创建NettyServerBootstrap,使用Spring注入需要的对象: ```java @Configuration public class NettyServerConfig { @Autowired private NettyServerInitializer nettyServerInitializer; @Value("${netty.server.port}") private int port; @Bean public EventLoopGroup bossGroup() { return new NioEventLoopGroup(); } @Bean public EventLoopGroup workerGroup() { return new NioEventLoopGroup(); } @Bean(initMethod = "bind", destroyMethod = "shutdownGracefully") public ServerBootstrap serverBootstrap() { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup(), workerGroup()) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(nettyServerInitializer); return serverBootstrap; } @Bean public InetSocketAddress inetSocketAddress() { return new InetSocketAddress(port); } } ``` 4. 在Controller中使用Netty的组件: ```java @RestController public class NettyController { @Autowired private ServerBootstrap serverBootstrap; @Autowired private InetSocketAddress inetSocketAddress; @GetMapping("/start") public void start() throws InterruptedException { ChannelFuture channelFuture = serverBootstrap.bind(inetSocketAddress).sync(); channelFuture.channel().closeFuture().sync(); } } ``` 这样就完成了Netty和Spring Boot的整合,并且使用了Spring的依赖注入来管理Netty的组件。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值