2011-04-14 64 views
12

Spring如何@Autowire beans:byName或byType?如果不可能,是否使用另一种模式进行了第二次试验?@Autowire默认模式

回答

24

如果使用@Autowired进行注释,它将注入具有匹配类型的bean(如果存在多个类型,则会抛出异常)。要指定名称,请使用@Qualifier注释。

3

弹簧@Autowire按类型。按名称接线也可以使用

@Resource(name = "id") 
1

@Autowired的默认模式为byType

+1

False时,@Autowired的缺省模式是 '不' 否自动装配是执行。所有对其他bean的引用都必须明确注入。这是默认模式。 – TheCyberXP 2015-01-02 18:09:21

+2

默认情况下,您引用autowire属性为'no',@ Autowired注解为byType – jarosik 2016-09-29 08:51:38

0

Autowired注释可变或设置器方法等效于XML属性autowire="byType"

XML属性autowire是defaut作为no

"no": 

The traditional Spring default. No automagical wiring. Bean references 
must be defined in the XML file via the <ref/> element (or "ref" 
attribute). We recommend this in most cases as it makes documentation 
more explicit.