Differences between @Autowired tags and @Resource labels in spring

Source: Internet
Author: User

Spring not only supports its own defined @autowired annotations, but also supports several annotations defined by the JSR-250 specification, such as: @Resource, @PostConstruct, and @predestroy.

1. @Autowired
@Autowired is provided by spring and needs to be imported
Package:org.springframework.beans.factory.annotation.Autowired;
Inject only according to Bytype.
2. @Resource
@Resource is automatically injected by ByName by default, which is provided by the Java EE and needs to be imported into the package:
Javax.annotation.Resource;
@Resource has two important attributes: Name and type, and spring resolves the name property of the @resource annotation to the Bean's
Name, and the type attribute resolves to the bean. So if you use the Name property, use the ByName auto-injection policy, and use the
The type attribute is used with the Bytype auto-injection policy. If neither name nor the type attribute is specified, the by is used through the reflection mechanism.
Name auto-injection policy.

@Resource Assembly Order
(1). If name and type are specified at the same time, the uniquely matched bean is found from the spring context to assemble, and an exception is thrown if it is not found;
(2). If name is specified, a bean matching the name (ID) from the context is assembled, and an exception is thrown if it is not found;
(3). If a type is specified, an exception is thrown when a unique bean with a matching match is found in the context, not found or multiple;
(4). If neither name is specified and type is not specified, the assembly is automatically byname; if there is no match, the fallback is
The original type is matched and automatically assembled if the match is made;
The function of @Resource is equivalent to @autowired, but @autowired is injected automatically by Bytype.
3. Usage differences
@Resource (name= "Loginservice")
Private Loginservice Loginservice;

@Autowired (Required=false) @Qualifier ("Loginservice")
Private Loginservice Loginservice;

(1) [email protected] and @resource can be used to assemble beans. Can be written on a field, or written on a setter method;
(2) [email protected] By default assembly by type, by default must require that dependent objects must exist, if you want to allow null values, you can set

Set its Required property to false, such as: @Autowired (Required=false).

If we want to use the name assembly can be used in conjunction with @Qualifier annotations;

(3) [Email protected] (this annotation belongs to the Java EE), the default installation name is assembled, the name can be specified by the Name property, if not
The Name property is specified, and when the annotation is written on a field, the default Fetch field name is the installation name lookup if the annotation is written on the setter method by default
Name of the assembly. When a bean matching the name cannot be found, it is assembled by type. However, it is important to note that if the Name property once refers to the
will only be assembled by name.

It is recommended to use @resource annotations on fields so that you do not have to write setter methods. And this annotation belongs to the Java EE, reducing the spring

Coupling, so that the code looks more elegant.

In addition, through the practice, also summed up a rule:

If you write @requied or @autowired on top of the set method, the program goes inside the set method.

However, if it is written on a field, it will not enter the set method.

Differences between @Autowired tags and @Resource labels in spring

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.