在一些情况下Servcie是Null,获取不到Impl

mac2024-10-17  54

有的时候我们在debug调试的时候,会遇到这种情况,获取不到Service 执行之后报如下错误:

java.lang.NullPointerException at com.dy.customer.demo.util.netty.TcpReadHandler$1.run(TcpReadHandler.java:74) at java.util.TimerThread.mainLoop(Timer.java:555) at java.util.TimerThread.run(Timer.java:505)

解决办法: 在加载方法的时候进行初始化::

代码如下: @Component public class TcpReadHandler extends ChannelInboundHandlerAdapter { private static TcpReadHandler tcpReadHandler ; @Autowired private AppModuleService appModuleService; @Autowired private SocketLogin socketLogin; @PostConstruct //通过@PostConstruct实现初始化bean之前进行的操作 public void init() { tcpReadHandler = this; tcpReadHandler.appModuleService = this.appModuleService; // 初使化时将已静态化的testService实例化 }

如何调用:

tcpReadHandler.appModuleService.saveModule(dto);

最新回复(0)