两个springboot的web应用,使用Thrift来调用,一个是“服务端”,另一个是“客户端”。
持久层方面,使用了JPA。
“客户端”通过Thrift来调用“服务端”的函数(transmitData)时,我需要通过JPA来将数据写入数据库,这时报了个空指针错误,我发现,如下:
@Controller
public class TransmitServiceImpl implements TransmitService.Iface
{
@Autowired
private ITransactionDataRepository TransactionDataRepository;//注入(实际情况是null)
...
@Override
public boolean transmitData(TransactionData transData) throws TException//通过Thrift调用的函数
{
...
TransactionDataRepository.save(...);//写入持久层(此时报空指针错误)
...
}
}
服务端Controller中TransactionDataRepository竟然是Null的。
后来我上午查资料,听说此时无法注入,只能通过new的方式。但是ITransactionDataRepository是抽象的,我无法实例化,这时候应该怎么处理?
如果整个ApplicationContext中没有
ITransactionDataRepository
的Bean,那么在启动时就会报无法找到ITransactionDataRepository Bean的错误,而不是在运行时报错。怀疑你的TransmitServiceImpl是你自己new出来的,不是通过ApplicationContext构建的。
Thrift不懂希望下面两个链接对你有帮助:
https://dzone.com/articles/bu...
https://github.com/bsideup/sp...