spring and @Resource difference in @Autowired

@Autowired and @Resource difference:

 

1, @Autowired @Resource and can be used to assemble bean. Field can be written on or not written in the setter method.

2, @Autowired default type press fitting (comment this industry are of the spring), by default in claim dependent objects must be present, if you want to allow null values, can set its required property is false, such as: @Autowired (required = false), if we want to use the name of the assembly can be combined @Qualifier annotations for use, as follows:

1
2
@Autowired () @Qualifier ( "baseDao" )
private BaseDao baseDao;

3, @ Resource (this annotation belongs to a J2EE), default assembled by name, the name can be specified by the name attribute, if you do not specify the name attribute, when the notes written on the field, by default take the field names for installation name lookup, if Notes written in the property setter methods to take default name for assembly. When that matches the name can not be found when assembled in accordance with the type of bean. However, note that if the name attribute if specified, will only be assembled by name.

1
2
@Resource (name= "baseDao" )
private BaseDao baseDao;

Recommended use: @Resource annotation on the field, so do not write the setter methods, and this comment is part of J2EE, reducing the coupling of the spring. Since this code is relatively elegant look.

Guess you like

Origin www.cnblogs.com/mww-NOTCOPY/p/11140150.html