What is the difference between @autowired and @resource annotations?
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.
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.
@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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
