In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge about "what is the difference between @Autowired and @Resource in Spring". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
I. Definitions
@Autowired
Mark class member variables, methods and constructors, and complete the work of automatic assembly.
@Resource
Semantically defined to identify a particular target component by its unique name, where the declared type is independent of the matching process.
If no name is explicitly specified, the default name is derived from the field name or the set method (get, set method). If used on a field, it takes the field name; if used on a setter method, it takes its attribute name (e.g. setProperty() method takes property as the attribute name).
II. Distinctions
In Spring Framework, if we need to inject other dependent objects in the Service layer, we usually use @Autowired or @Resource annotations, but they are different, for example,@Autowired is strongly coupled with Spring Framework, if it is changed to other frameworks,@Autowired will not work. @Resource is provided by JSR-250, which is a Java standard supported by most frameworks.
Next, let's summarize the difference between @Autowired and @Resource:
(a) The attributes contained are different
@Autowired contains only one parameter: required, indicating whether to enable automatic injection, the default is true. @Resource has seven parameters, the two most important of which are name and type. As follows:
public @interface Autowired { /** * Whether to enable automatic injection, sometimes we do not want to use the automatic assembly function, you can set this parameter to false. */ boolean required() default true; } public @interface Resource { /** * Name of bean */ String name() default ""; String lookup() default ""; /** * Java classes, resolved to bean types */ Class type() default java.lang.Object.class; enum AuthenticationType { CONTAINER, APPLICATION } /** * authentication type */ AuthenticationType authenticationType() default AuthenticationType.CONTAINER; /** * Can a component be shared with other components */ boolean shareable() default true; String mappedName() default ""; /** * described */ String description() default "";}
(2)@Autowired defaults to automatic assembly by type, while @Resource defaults to automatic assembly by name.
@Autowired If you want to use byName, you need to use @Qualifier together. @Resource is automatically assembled with byName if name is specified, and byType if type is specified.
(3) Different places where annotations are applied
@Autowired can be used on constructors, methods, parameters, member variables and annotations, while @Resource can be used on classes, member variables and methods.
(iv) Different sources
@Autowired is a Spring defined annotation, and @Resource is a JSR-250 defined annotation. So @Autowired can only be used under Spring framework, while @Resource can be used with other frameworks.
(V), assembly order is different
The assembly sequence for @Autowired is as follows:
@Autowired defaults to matching by byType first. If multiple beans are found, they are matched by byName. If there are more than one bean, an exception is reported.
The assembly sequence of @Resource is as follows:
If both name and type are specified, the process is as follows:
If name is specified, the process is as follows:
If only the name of @Resource annotation is specified, then search for bean with name attribute equal to bean element by name after name.
If type is specified, the process is as follows:
If you specify only the type attribute of @Resource annotation, you will find a unique bean matching the type from the context for assembly. If you cannot find it or find more than one bean, an exception will be thrown.
If neither name nor type is specified, the process is as follows:
If neither the name attribute nor the type attribute is specified, the lookup is automatically performed byName. If no matching bean is found, it falls back to an original type to look up and inject if found.
"What are the differences between @Autowired and @Resource in Spring?" is introduced here. Thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.