首页 技术 正文
技术 2022年11月15日
0 收藏 481 点赞 2,835 浏览 1148 个字

其他乱七八糟配置就不扯了,先上项目结构图

Springboot 测试类没有找到bean注入

配置好参数后我再src/test/java类测试访问数据库时发现bean没有正确的注入。值得注意的是,这个项目的启动类是叫App.java

所以我们必须在这个测试类上面加上注解:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)

注意:SpringBoot(classes = App.class) classes后面跟的是启动类的class,千万不要随便抄网上的配置,写一些Application.class之类的,这种Application之类的类名和一些官方包里的类名一样,容易引入错误的包。

刚开始发现这个问题疯狂去网上看别人的配置文件是怎么写的,试了一天都没用,后来静下心来,把错误信息copy出来文本里仔细看

org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'com.springboot.LibrarySystem.mapper.UserMapperTest':
Unsatisfied dependency expressed through field 'userMapper';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.sb.LibrarySystem.mapper.UserMapper'
available: expected at least bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

还是从这个Test类下手

本来我的类是这样的:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class UserMapperTest {
}

修改后就是这样,和我的启动类的类名是一致的:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class UserMapperTest {

完美解决!

如果百度的时候,发现查看的问题越来越深,越来越偏离最开始的问题,那十有八九是方向偏了,重新整理一下,重新开始吧

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,498
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,136
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,301