In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
I would like to share with you the example analysis of @ Autowired and @ Qualifier annotations in Spring. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it together.
1.@Autowired automatic injection of three knowledge points of @ Autowired and @ Qualifier annotations
The default is byType. If multiple types match, then byName is used for injection.
two。 If you want to use byName injection directly
Then you need to add @ Qualifier ("userService") at the bottom of the @ Autowired annotation, with the name of the bean to be injected in parentheses, as shown in the figure
3. If you do not specify the name of the bean created by Spring
(1) the default name is the class name, but the first letter is lowercase, such as UserService class. The name of the created bean is userService.
(2) if the first two or more letters of the class name are capitalized, then the name of bean is the class name, such as TUserService class. The name of the bean created by default is TUserService.
The difference between @ Autowired @ Qualifier @ Resource
@ Autowired
Comments of spring, which is injected by type by default
@ Qualifier
Spring annotations are injected by name. Generally, when two or more bean appear, you don't know which one to inject, which is used as a modifier for @ Autowired ().
@ Resource
@ Resource (this annotation belongs to J2EE). Default injection is name, which can be selectively injected through name and type attributes.
Take a look at the chestnuts
1.@Autowired
Write a User class to instantiate a User Bean to run the program using @ Autowired injection in controller
Access interface
Note that @ Autowired by default required=true is used when the injected object is not instantiated. If you want to allow the null value, you can set its required property to false,@Autowired (required=false).
2.@Qualifier
When instantiating Bean, instantiate two as shown in the figure
At this time, you will find that the error in the injection is not clear which bean error to inject is as shown in the following figure
At this point, we can use @ Qualifier to match @ Autowire for injection.
First, when instantiating bean, specify the name injection using the corresponding name injection in the following figure
Normal access after reboot
3.@Resource
The name is assembled by default according to the name, which can be specified through the name attribute. If no name attribute is specified, when the note is written on the field, the field name is looked up by name by default, and if the note is written on the setter method, the attribute name is used for assembly by default.
Assemble by type only when a bean matching the name is not found. It is important to note, however, that once the name attribute is specified, it will only be assembled by name.
It can be accessed normally after the injection startup project is specified through the name attribute.
Initialize a bean,name=user2 without specifying name
He first injects Bean according to the lowercase user of the class name as name, and finds that there is no instance of that name, and then automatically matches according to the type, so that the running program can be accessed normally without error.
The above is all the content of the article "sample Analysis of @ Autowired and @ Qualifier annotations in Spring". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.
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.