如何在Netty中注解使用Service或者Mapper
作者:sinat_35938012
这篇文章主要介绍了如何在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
@Component public 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
@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)); } }); } }
Netty handler注入service为空
注入方式
@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类即可
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。