这篇文章将为大家详细讲解有关SpringBoot Test类注入失败怎么办,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。如下所示本来 bookService的引用一直是null。导致每次测试都报空指针异常
这篇文章将为大家详细讲解有关SpringBoot Test类注入失败怎么办,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
本来 bookService的引用一直是null。
导致每次测试都报空指针异常。
然后现在继承相应的 ApplicationTests类,然后使用@Component将该类注册为组件。就可以正常注入了。
补充:关于springboot test @Mapper ,@Autiwired注入无效的问题
@springBootTest()@RunWith(SpringRunner.class)public class ProductMapperTest { @Autowired ProductMapper productMapper;
为了给mapper接口 自动根据一个添加@Mapper注解的接口生成一个实现类
怎么注入都是失败,ProductMapper 使用@Mapper 注解,这个不能注入到spring 容器中(其中原因还是不了解)。
@Autowired 注入不进去的。
@Repository@Mapperpublic interface ProductMapper {
这下能注入容器中了。
关于“SpringBoot Test类注入失败怎么办”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
--结束END--
本文标题: SpringBoot Test类注入失败怎么办
本文链接: https://www.lsjlt.com/news/247817.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
2024-05-15
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0