@Resource and @Autowired difference

【参考】https://blog.csdn.net/weixin_40423597/article/details/80643990

@Autowired default according to the type assembled by default in claim dependent objects must be present, if the null value is allowed, can set its required property is false, if we want to be assembled with the name, you need to use @Qualifier ( "name")

 

@Resource default assembled by name, the name can be specified by the name attribute value, if you do not specify the name attribute, when the notes written on the field, take the default name lookup field names for installation, if the notes written on the setter methods to take default properties It was assembled. When that matches the name can not be found in accordance with the type of bean will be assembled. However, note that if the name attribute if specified, will only be assembled by name

 

@Resource assembly sequence

  1. If you specify the name and type, from the Spring context to find a unique match of bean assembled, can not find an exception is thrown

  2. If a name is specified, from the context, find the name (id) match bean assembled, can not find an exception is thrown

  3. If the type is specified, from the context that matches the type of bean find a unique assembled, can not find or find more, will throw an exception

  4. If neither name, nor the specified type, according to the automatic assembly byName manner; if not assembled, matching the backoff type is a primitive

Guess you like

Origin www.cnblogs.com/knightdreams6/p/11527931.html