First, the range of @resource is larger than the @autowire.
- First find all the beans that meet the criteria by type
- Determine the bean length, if not, to determine if an exception is thrown based on the required property in @autowired (default is True)
- If more than one, then try to find the optimal one, if the optimal is not found, then throw an exception
- If there is only one, use this bean directly
If @autowire is marked on the setter method, the default is to inject by the parameter name instead of the property name behind the setter, and if there is no name for the parameter, press Bytype, and the following is injected by TX1 parameter name
Public void setTx2 (txinterface tx1) {this. tx2 = TX1;}
If Autowire is labeled above the field, the field name is used directly. To find, such as
@autowire Private String username;
If the label is according to @resource, it will first match according to name, if the match is not in degenerate into bytype
- Gets the name of the element that determines whether the bean for this name exists beanfactory
- If present, use this name to query directly
- Otherwise degenerate to the default Autowire lookup mode
- This is said to be the name of element because it has 2 locations. The first is the Name property configured in the Resouce annotation, and the second is the Setter name or field name (depending on where the @resource is configured), which is called the setter name, not the property name, which is where you need to be aware
It is important to note that if you find the bean based on resource's name when using resource, but the bean is not the type of bean that is required, then the type mismatch error will be reported. That is, when spring is looking, there is no guarantee of type judgment, that is, you configure a tx2 bean of name, but the type is TxInterface2 instead of txinterface, then spring will report the exception at a later time instead of fallback. However, this is not the case with the autowired annotation, because it only looks for the bean in the case where the type is satisfied
On the difference between @autowire and @resource