您好,登录后才能下订单哦!
这篇文章主要介绍了怎么在Netty中注解使用Service或者Mapper,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
SpringBoot搭配Netty使用,在Handler中注解使用Service/Mapper 一直 为null。起初使用 SpringBoot启动类继承ApplicationContextAware,再写一个静态方法获取Bean来解决。
放在服务器上跑了几个小时就出现了问题,好像是该方案造成的。
另辟蹊径,再看网上给出的答案,因为 Handler 是 new出来的,不被Spring管理,所以注解根本就没有什么作用。
思前想后,(有BUG,下文修正解决:觉得可以给Handler添加@Component 注解交给Spring 管理,再通过构造方法把 Handler 传递给Server。结构代码):
@Component public class OBDHandler extends ChannelInboundHandlerAdapter { private final Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private OBDMapper mOBDMapper; }
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); } }); } }
@Configuration public 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)); } }); } }
@Slf4j @Component public 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”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。