这篇文章主要介绍了怎么在Netty中注解使用Service或者Mapper,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
Netty注解使用Service或Mapper
SpringBoot搭配Netty使用,在Handler中注解使用Service/Mapper 一直 为null。起初使用 SpringBoot启动类继承ApplicationContextAware,再写一个静态方法获取Bean来解决。
放在服务器上跑了几个小时就出现了问题,好像是该方案造成的。
另辟蹊径,再看网上给出的答案,因为 Handler 是 new出来的,不被Spring管理,所以注解根本就没有什么作用。
思前想后,(有BUG,下文修正解决:觉得可以给Handler添加@Component 注解交给Spring 管理,再通过构造方法把 Handler 传递给Server。结构代码):
Handler
@Componentpublic class OBDHandler extends ChannelInboundHandlerAdapter { private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private OBDMapper mOBDMapper;}
Server
public class OBDServer { private Logger log = LoggerFactory.getLogger(getClass()); private OBDHandler obdHandler; private int port; public OBDServer(int port,OBDHandler obdHandler) { this.port = port; this.obdHandler = obdHandler; } private void bind() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(group)// .channel(NioServerSocketChannel.class) // .childHandler(new ChannelInitializer<SocketChannel>() { // @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024*1024,0,4)); ch.pipeline().addLast(obdHandler); } }); }}
创建Server
@Configurationpublic class NettyConfig { @Autowired private OBDHandler obdHandler; @Bean public OBDServer obdServer(){ OBDServer obdServer = new OBDServer(7788,obdHandler); obdServer.startServer(); return obdServer; }}
二次修订
并发需要创建多个Handler,而被Spring托管的单例Handler会造成以下错误:
is not a @Sharable handler, so can't be added or removed multiple times.
最终通过在Server中注入 Service/Mapper,再通过Handler构造传递:
public class OBDServer { @Autowired private OBDMapper mOBDMapper; public OBDServer(int port) { this.port = port; } private void bind() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(group)// .channel(NioServerSocketChannel.class) // .childHandler(new ChannelInitializer<SocketChannel>() { // @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024*1024,0,4)); ch.pipeline().addLast(new OBDHandler(mOBDMapper)); } }); }}
Netty handler注入service为空
注入方式
@Slf4j@Componentpublic class BootNettyChannelInboundHandlerAdapter extends ChannelInboundHandlerAdapter { public static BootNettyChannelInboundHandlerAdapter bootNettyChannelInboundHandlerAdapter; //1.正常注入[记得主类也需要使用@Component注解] @Autowired private DeviceUpService deviceUpService; //2.初始化构造方法一定要有 public BootNettyChannelInboundHandlerAdapter() { } //3.容器初始化的时候进行执行-这里是重点 @PostConstruct public void init() { bootNettyChannelInboundHandlerAdapter = this; bootNettyChannelInboundHandlerAdapter.deviceUpService = this.deviceUpService; }}
完成以上步骤后通过
bootNettyChannelInboundHandlerAdapter.deviceUpService
调用service类即可
感谢你能够认真阅读完这篇文章,希望小编分享的“怎么在Netty中注解使用Service或者Mapper”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!