@Autowired与@Resource的区别
@Autowired默认是通过byType自动注入,但是@Autowired如果通过byType找不到属性,也会通过byName自动注入,下例图集示例一为byType自动注入时报错。





运行代码后会提示:
available: expected single matching bean but found 2: demoImp1,demoImp2
@AutoWired通过byName自动注入,如图集例二


以上操作也是可以正常输出结果的。
@Resource操作 默认是通过byName(属性名称)自动注入,如下图中的demoImpOne/demoImpTwo


或者


综上所述, @Autowired与@Resource的区别:
Autowired既有byType自动注入功能,也有byName自动注入功能,但默认是byType。Resource默认byName.
First you jump off the cliff and build your wings on the way down.again and again, time will show you the best result.

浙公网安备 33010602011771号