如何在Netty中注解使用Service或者Mapper
程序员文章站
2022-06-09 19:06:29
目录netty注解使用service或mapperhandlerserver创建server二次修订netty handler注入service为空注入方式netty注解使用service或mappe...
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类即可
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。