Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
When we use Java development, we use assembly annotations to inject objects.
The most commonly used annotations are @resource and @autowire. Many people do not care about the usage of these two annotations in their daily work. In fact, it is because in normal Java development, there will not be problems if the two annotations are replaced.
In order to avoid accidents, it is better to learn in advance, let’s talk about the difference between the two.
Assembly way
The Autowire annotation, by default, assembles by type, but its configuration properties require that the assembled object be allowed to be NULL, which can also be modified by configuration.
Resource annotations, which are assembled by object name by default, are then matched by type when no bean with a matching name is found.
The other way
If the Autowire annotation also wants to use assembly by object name, it needs to be configured through the @qualifier annotation.
The @Resource annotation automatically handles assembly using both the object name and the object type without configuration, just checking whether the bean corresponding to the object name can be obtained first. If not, the assembly will be based on the object type.
In the development of the project, in fact, the program should be written in accordance with the team’s coding rules. Many people will recommend the use of the @Resource annotation for the use of the two annotations, which can be used for reference. Although it is better than Autowire annotation in function, the @Resource annotation is relatively expensive.
Every day to write down their work need baidu problems, always believe that good things are about to happen.