Home  >  Article  >  Java  >  What is the difference between @autowired and @resource annotations?

What is the difference between @autowired and @resource annotations?

青灯夜游
青灯夜游Original
2020-11-09 10:35:1587398browse

Difference: 1. The @Autowired annotation is provided by Spring and is only injected according to byType; the @resource annotation is provided by J2EE and is automatically injected according to byName by default. 2. @Autowired is assembled by type by default, and @Resource is assembled by name by default.

What is the difference between @autowired and @resource annotations?

Related recommendations: "Java Video Tutorial"

Spring not only supports its own @Autowired annotations , also supports several annotations defined by the JSR-250 specification. For example: @Resource, @PostConstruct and @PreDestroy

1, @Autowired

are provided by Spring and are only injected according to byType

2 , @Resource

is provided by J2EE, and is automatically injected by byName by default.

@Resource has two important attributes: name and type

Spring annotates @Resource The name attribute resolves to the name of the bean, and the type attribute resolves to the bean type. So if you use the name attribute, the byName automatic injection strategy is used, and if the type attribute is used, the byType automatic injection strategy is used. If neither name nor type attribute is specified, the byName automatic injection strategy will be used through the reflection mechanism.

@Resource assembly sequence:

(1) If name and type are specified at the same time, the only matching bean will be found from the Spring context for assembly. If it is not found, an exception will be thrown

(2) If name is specified, the bean matching the name (id) will be searched from the Spring context for assembly. If no bean is found, an exception will be thrown

(3) If type is specified, then Find the only bean with a matching type from the Spring context for assembly. If no bean is found or more than one is found, an exception will be thrown

(4) If neither name nor type is specified, the byName method will be automatically used. Perform assembly. If there is no match, fallback to a primitive type for matching, and autowire if there is a match.

The function of @Resource is equivalent to @Autowired, except that @Autowired is automatically injected according to byType.

3. Differences in usage

(1) Both @Autowired and @Resource can be used to assemble beans, and both can be written in the field or setterOn the method

(2)@Autowired is assembled by type by default. By default, the dependent object must exist. If you want toallow null values , you can set its required attribute to false. If you want to use name assembly , you can use it in conjunction with the @Qualifier annotation .

(3) @Resource, is assembled according to the name by default. The name can be specified through the name attribute . If the name attribute is not specified, When an annotation is written on a field, defaults to the field name for name search. If the annotation is written on the setter method, the attribute name will be used for assembly by default. When cannot find a bean matching the name, is assembled according to the type . However, it should be noted that if the name attribute is specified, it will only be assembled according to the name.

It is recommended to use the @Resource annotation on the field, so that there is no need to write a setter method, and this annotation belongs to J2EE, reducing the coupling with Spring.

What is the difference between @autowired and @resource annotations?
@Resource Note

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of What is the difference between @autowired and @resource annotations?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn