On the difference between @autowire and @resource

Source: Internet
Author: User

First, the range of @resource is larger than the @autowire.

    1. First find all the beans that meet the criteria by type
    2. Determine the bean length, if not, to determine if an exception is thrown based on the required property in @autowired (default is True)
    3. If more than one, then try to find the optimal one, if the optimal is not found, then throw an exception
    4. 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

    1. Gets the name of the element that determines whether the bean for this name exists beanfactory
    2. If present, use this name to query directly
    3. Otherwise degenerate to the default Autowire lookup mode
    4. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.