您好,登錄后才能下訂單哦!
這篇文章主要介紹了怎么在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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。