Hi everyone, my name is Mic.
A fan who has worked for 2 years asked me a question in Spring.
I hope I can analyze it from a different perspective and then crush the interviewer.
This question is: Difference between "@Resource and @Autowired".
Here's a look at the answers from ordinary people and experts
Ordinary people:
The difference between @Resource and @Autowired I think is one way of their injection
@Resource is injected according to name, @Autowired is injected according to type.
Expert:
OK, interviewer.
The two annotations @Resource and @Autowired are used to implement Bean dependency injection in the Spring ecosystem.
Let me talk about the two annotations @Autowired and @Resource respectively.
Flashed [@Autowired's role detailed] a few words.
First of all, @Autowired is an annotation provided in Spring. By default, Bean dependency injection is implemented according to the type.
There is a required
attribute in the @Autowired annotation. The default value is true, which means that the injection of bean instances is mandatory.
When the application starts, if the corresponding type of bean does not exist in the IOC container, an error will be reported.
Of course, if you don't want automatic injection, you can set this property to false.
Secondly, if there are multiple Bean instances of the same type in the Spring IOC container. Because the @Autowired annotation is injected into the Bean instance according to the type
So when Spring starts, it will prompt an error, which probably means that only a single instance bean can be injected.
However, there are more than one in the IOC container, which causes the injection to fail.
Of course, for this problem, we can use the two annotations @Primary or @Qualifier to solve.
@Primary represents the main bean. When there are multiple beans of the same type, the bean declared with @Primary is preferred.
The role of @Qualifier is similar to conditional filtering, it can find the target bean that needs to be assembled according to the name of the bean.
Flashed the words [Detailed explanation of the role of @Resource].
Next, let me explain the @Resource annotation.
@Resource is an annotation provided by JDK, but Spring provides functional support for this annotation in implementation.
Its usage is exactly the same as @Autowired. The biggest difference (as shown in the figure) is that @Resource can support both ByName and ByType injection methods.
If you use name, Spring will perform dependency injection based on the name of the bean. If you use type, Spring will perform dependency injection based on the type.
If neither of the two attributes is configured, it will be matched according to the defined attribute name first, and if it does not match successfully, it will be matched according to the type. If neither of them match, an error will be reported.
Finally, let me summarize.
- @Autowired is matched according to type, @Resource can be matched according to name and type, the default is name match.
- @Autowired is an annotation defined by Spring, @Resource is an annotation defined in the JSR 250 specification, and Spring provides support for the JSR 250 specification.
- If @Autowired needs to support name matching, it needs to be implemented with @Primary or @Qualifier.
The above is my understanding of the problem.
Summarize
You can pay attention to the answer of the master part, his logical structure is very clear.
He is very intuitive to tell the interviewer the difference between the two annotations, and at the same time explains more differences based on the characteristics of the two annotations.
Finally, a brief summary is made.
You can refer to similar answers when you are interviewing.
Friends who like my works remember to like, favorite and follow.
Copyright notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated. Please indicate the source forMic带你学架构
!
If this article is helpful to you, please help to follow and like, your persistence is the driving force for my continuous creation. Welcome to follow the WeChat public account of the same name to get more technical dry goods!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。