Autowired and Resource difference

@Autowired assembly can be used with @Resource bean. Field can be written on or not written in the setter method.
@Resource default is to assemble injected by name, only if can not find a name that matches the type of bean will be assembled in accordance with the injection;
@Resource assembly sequence:
1. If both the name and type, from the Spring context found the only match 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 specified type, the only find from the context that matches the type of bean assembled, you can not find or find more, throw exceptions
4, if neither the name, but do not specify a type, then automatically assembled according to byName way; if there is no match, matching the fallback is a primitive type, if a match is automatically assembled;
@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, may be provided it's required property is false, such as: @Autowired (required = false), if we want to use the name of the assembly can be combined @Qualifier carried out using annotations

Published 32 original articles · won praise 7 · views 8521

Guess you like

Origin blog.csdn.net/weixin_44516119/article/details/104477939