Spring Boot Netty 中 @Autowired, @Value 为空解决

转载自:https://www.cnblogs.com/victorbu/p/10692867.html

叙述

使用 Spring Boot + Netty 新建项目时 Handler 中的 @Autowired, @Value 注解的始终为空值

解决方案

@Component // 1. 添加 @Component 注解
public class TestHandler extends ChannelInboundHandlerAdapter {

    private static TestHandler testHandler; // 2. 定义本类的静态对象

    @Autowired
    TestService testService;

    @PostConstruct // 3. 添加 @PostConstruct 注解的方法
    public void init(){
        testHandler = this;
    }


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {

        ByteBuf buf = (ByteBuf) msg;

        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);

        testHandler.testService.handlerMessage(bytes); // 4. 使用

    }

}
发布了27 篇原创文章 · 获赞 32 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/lizhengyu891231/article/details/104605674