2

SpringBoot 求教这里为什么 Autowire 找不到 bean

 3 years ago
source link: https://www.v2ex.com/t/801316
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
SpringBoot 求教这里为什么 Autowire 找不到 bean

V2EX  ›  程序员

SpringBoot 求教这里为什么 Autowire 找不到 bean

  movq · 10 小时 24 分钟前 · 878 次点击

d4fYBeKMEyjN8sv

如果测试程序使用这个,就能找到 dog 这个 bean

2JscVOj1gvk6WGb

第 1 条附言  ·  6 小时 34 分钟前

谢谢大家,已解决,因为我使用的是 junit4,所以根据 5 6 楼的解答,使用 @RunWith(SpringRunner.class)即可

Xhack

Xhack   9 小时 59 分钟前

源码包和 test 不是一个包吧

hiw2016

hiw2016   8 小时 10 分钟前

第二张图「如果测试程序使用这个」,没太看懂这句话

hiw2016

hiw2016   8 小时 10 分钟前

@hiw2016 #2 建议直接把项目放出来

DeadLion

DeadLion   8 小时 7 分钟前   ❤️ 1

看来是个新手了

你程序启动会自动加载加载 spring 一系列的上下文,包括各种 bean

但是 test 包不会自动加载的,但是我记得有个注解可以加到测试类上就能帮你加载 spring 上下文了。

NotFoundEgg

NotFoundEgg   7 小时 59 分钟前   ❤️ 1

不知道你用的是 Junit4 还是 5
如果是 Junit4 测试类上打一个 @RunWith(SpringRunner.class)

leonme

leonme   6 小时 59 分钟前 via iPhone   ❤️ 1

@RunWith(SpringRunner.class)
@SpringBootTest

movq

movq   6 小时 32 分钟前

@Xhack 是一个包吧,都是 com.kuang 里面的,com.kuang 里的测试程序看不见 com.kuang.pojo 里面的类吗?
@hiw2016 意思就是我在第二章图里面那个 spring application 里面,使用 @Autowired 注解可以找到 dog 这个 bean

Xhack

Xhack   6 小时 7 分钟前

@movq 可能没有加载到 test 包里,加个注解

关于   ·   帮助文档   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   2100 人在线   最高记录 5497   ·  

创意工作者们的社区

World is powered by solitude

VERSION: 3.9.8.5 · 20ms · UTC 12:09 · PVG 20:09 · LAX 05:09 · JFK 08:09
♥ Do have faith in what you're doing.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK