spring - the difference Springmvc in @Autowired notes and annotations @Resource

Spring not only supports @Autowired annotation own definition, but also supports several notes by the JSR-250 specification defined, they are @ Resource, @ PostConstruct and @PreDestroy.
  @Resource effect equivalent to @Autowired, but @Autowired by byType automatically injected, while @Resource default automatically injected by byName nothing. @Resource has two attributes are more important, points that name and type, Spring will @Resource annotation name attribute resolves to the bean's name, the type attribute is resolved to the type of bean. So if you use the name attribute is used byName automatic injection strategy, and is used when using the type attribute byType automatic injection strategy. If neither type attribute name is assigned, the automatic injection strategy case byName by using reflection.
  @Resource assembly sequence
  1. If both 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) matching bean assembled, can not find an exception is thrown
  3. If the type is specified, the only bean from the context that matches the type found assembled, can not find or find more, will throw an exception
  4. If not both Specifies the name, and no specified type, according to the automatic assembly byName manner; if there is no match, then the match is a backoff primitive type, if a match is automatically assembled;
@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 (This comment belongs to 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:
@Autowired () @ Qualifier ( "BaseDao")
privateBaseDao BaseDao;
3, @ Resource (this annotation belongs to a J2EE), the name of the default installation assembled, the name can be specified by the name attribute, if you do not specify the name attribute, when writing notes when on the field, take the default name lookup field names for installation, if the notes written on the attribute 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.
@Resource (name = "BaseDao")
privateBaseDao BaseDao;
recommended: @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/tieandxiao/p/10931428.html