背景:
做了一个TCP服务器来接入智能设备,然后需要将设备实时发送的定位等关键信息存储到数据库。为了考虑将来可能对外提供rest接口,采用将TCP服务器集成到SpringBoot框架,当然,也是为了能最快利用mybatis框架实现数据访问,然后依次解决了如何启动,如何注销等各种问题,然后在TCP服务器消息处理时,需要写数据库,直接调用DAO层,编译报错。改为调用Service层,编译正常,运行到调用的地方,报空指针异常,跟踪到异常位置,发现service为空,也就是按照之前controller层通过@Autowired注入service层失效。
解决方案:
1.上代码
@Component
public class ServerHandler extends IoHandlerAdapter {
@Autowired
protected HealthDataService healthDataService;
private