Netty Handler无法依赖注入 注入service对象失败

@Component //第一步 加入spring容器
public class FpgaScoketServerHandler extends SimpleChannelInboundHandler<String>{

    public static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    
    @Autowired //第二步 把dao加载进来
    SystemWebSetDao systemWebSetDao;
    //第三步
    private static FpgaScoketServerHandler fpgaScoketServerHandler;
    
    
    @PostConstruct // 第4步 添加 @PostConstruct 注解的方法
    public void init() {
    	fpgaScoketServerHandler = this;
    }

然后就可以fpgaScoketServerHandler .systemWebSetDao.**使用

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在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. 创建Netty的ServerBootstrap,使用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的组件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦里藍天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值