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's the difference between @ Autowired and @ Resource in Spring?

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points about the difference between @ Autowired and @ Resource of Spring. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Before we begin to explain, we need to understand that both annotations are used to complete the assembly of components, that is, dependency injection (DI) is used to complete the assembly assignment of dependencies between components in the ioc container.

@ Autowired comment

Source: @ Autowired provides comments for Spring. You need to import the package org.springframework.beans.factory.annotation.Autowired.

Note: the default policy adopted by @ Autowired is injection by type (by-type). It is required that there must be an object of this type in the container. If not, an error will be reported and an exception will be thrown. You can also tell the container that you don't have to inject it by setting it to @ Autowired (required = false).

Example:

Public class StudentController {@ Autowired private StudentServer studentServer;}

As shown in the code above, the assembly will go to the spring container to find the class of type StudentServer and inject it. This creates a problem. When there are multiple objects of the same type in the container, it is impossible to choose which one to inject, resulting in an error. In this case, we can specify which object to assemble by @ Qualifier ("beanname").

Public class StudentController {@ Autowired @ Qualifier (name= "studentServer") private StudentServer studentServer;}

The @ Qualifier annotation tells spring to assemble the StudentServer object. At this point, we can successfully inject the right object.

@ Resource comment

Source: the @ Resource annotation is provided by J2EE and needs to be imported into the package javax.annotation.Resource.

Description: @ Resource can set by-name (by name) and by-type (by type) for automatic assembly. If it is not specified, it is automatically injected according to ByName by default.

Example:

Public class StudentController {@ Resource private StudentServer studentServer;}

If no name is specified and no type is specified, the annotation is automatically assembled in by-name mode, and if it matches, it is automatically assembled. If there is no match, look for it according to by-type, and if no match is found, an exception is thrown.

Public class StudentController {@ Resource (name= "studentServer") private StudentServer studentServer;}

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.

Public class StudentController {@ Resource (type= "StudentServer") private StudentServer studentServer;}

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

Public class StudentController {@ Resource (name= "studentServer", type= "StudentServer") private StudentServer studentServer;} public class StudentController {@ Resource (name= "studentServer", type= "StudentServer") private StudentServer studentServer;}

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

These are all the contents of the article "what's the difference between Spring's @ Autowired and @ Resource". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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

Development

Wechat

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

12
Report