spring 中工具类使用@Autowired

@Component
public class MeterNettyServerHandler extends SimpleChannelInboundHandler {

	Mcallback mcallback;

	public void addCallback(Mcallback mcallback) {
		this.mcallback = mcallback;
	}

	private static MeterNettyServerHandler nettyServerHandler;

	@Autowired
	private MeterService meterService;

	@PostConstruct
	public void init() {
		nettyServerHandler = this;
		// nettyServerHandler.meterService = this.meterService;
	}

	/**
	 * 读客户端数据
	 */
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
		// 使用ByteBuf收发数据包,不使用编码解码器
		ByteBuf buffer = (ByteBuf) msg;
		byte[] requestBytes = new byte[buffer.readableBytes()];
		buffer.readBytes(requestBytes);// 复制内容到字节数组bytes
		String requestStr = HexConvert.BinaryToHexString(requestBytes);
		nettyServerHandler.meterService.handleDataPacket(requestBytes, requestStr, ctx);
		// handleDataPacket(requestBytes, requestStr, ctx);
		ctx.flush();

	}
	…
	
	…
}
 
  

spring 中自定义工具类 解决@Autowired为null

你可能感兴趣的:(spring 中工具类使用@Autowired)