Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between @ Resource and @ Autowired in Spring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about the difference between @ Resource and @ Autowired in Spring. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Differences between Spring annotations @ Resource and @ Autowired

@ Resource and @ Autowired are both used for bean injection. In fact, @ Resource is not a Spring annotation. Its package is javax.annotation.Resource and needs to be imported, but Spring supports the injection of this annotation.

1. Common ground

Both can be written on fields and setter methods. If both are written on the field, there is no need to write the setter method.

2. Differences

(1) @ Autowired

The annotations provided by @ Autowired for Spring require the import package org.springframework.beans.factory.annotation.Autowired; to be injected only according to byType.

Public class TestServiceImpl {/ / the following two @ Autowired can be used in only one way @ Autowired private UserDao userDao; / / for @ Autowired public void setUserDao (UserDao userDao) {/ / methods for attributes this.userDao = userDao;}}

复制代码

The @ Autowired annotation assembles dependent objects by type (byType). By default, it requires that dependent objects must exist. If null values are allowed, you can set its required property to false. If we want to assemble by name (byName), we can use it with the @ Qualifier annotation. As follows:

Public class TestServiceImpl {@ Autowired @ Qualifier ("userDao") private UserDao userDao;}

(2) @ Resource

@ Resource is automatically injected according to ByName by default, which is provided by J2EE. You need to import the package javax.annotation.Resource. @ Resource has two important attributes: name and type, while Spring resolves the name attribute of the @ Resource annotation to the name of bean, while the type attribute resolves to the type of bean. Therefore, if the name attribute is used, the automatic injection policy of byName is used, and the byType automatic injection policy is used when the type attribute is used. If neither the name nor the type attribute is specified, the byName automatic injection strategy will be used through the reflection mechanism.

Public class TestServiceImpl {/ / the following two @ Resource can be used for @ Resource (name= "userDao") private UserDao userDao; / / for @ Resource (name= "userDao") public void setUserDao (UserDao userDao) {/ / this.userDao = userDao on the setter method for attributes. }} Note: it is best to put @ Resource on the setter method, because this is more in line with the object-oriented idea of manipulating properties through set and get rather than directly.

@ Resource assembly sequence:

① if both name and type are specified, a unique matching bean is found in the Spring context to assemble, and an exception is thrown if it is not found.

② if name is specified, a bean with a matching name (id) is found in the context for assembly, and an exception is thrown if it is not found.

③ if type is specified, a unique bean with a similar match is found in the context to assemble. If no or more than one is found, an exception will be thrown.

④ automatically assembles in byName mode if neither name nor type is specified; if there is no match, it falls back to an original type for matching, and if there is a match, it automatically assembles.

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

This is the difference between @ Resource and @ Autowired in Spring shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report