In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people are at a loss about how to find a matching Bean by type when Spring is injected into a single value. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Introduction
I often write the following code:
@ Autowired private AService aservice
I wonder if you are also curious about how Spring found Bean of type AService. Let's talk about this-> how to find a matching Bean by type during single-valued injection.
How to find matching Bean by type during single-value injection
It's very simple, the core is only three steps.
1. Find all the bean that matches the type, if only one is returned directly.
Spring is implemented in the DefaultListableBeanFactory.findAutowireCandidates method. Some of the source codes are as follows:
String [] candidateNames = BeanFactoryUtils .beanNamesForTypeIncludingAncestors (this, requiredType, true, descriptor.isEager ())
The purpose of this beanNamesForTypeIncludingAncestors is to get all matching beanName of type requiredType (AService) (including ancestor BeanFactory).
What if the beanNamesForTypeIncludingAncestors is implemented internally? I summed up the following brief logic:
Go through all the BeanDefinition and get all the BeanName.
For all BeanName, first try to get a single case for matching, and then match with Bean Definition if there is no match.
When matching, if Bean is FactoryBean, try to match the actual Bean produced by FactoryBean first, and then match it with FactoryBean if it does not match.
two。 When multiple Bean matches, there is a preference, and the preferred bean is returned.
DefaultListableBeanFactory.determinePrimaryCandidate implements the logic of filtering the preferred Bean, and the core method is isPrimary, which determines whether the current Bean is the preferred Bean. The source code is as follows
Protected boolean isPrimary (String beanName, Object beanInstance) {if (containsBeanDefinition (beanName)) {return getMergedLocalBeanDefinition (beanName). IsPrimary ();} BeanFactory parent = getParentBeanFactory (); return (parent instanceof DefaultListableBeanFactory & & ((DefaultListableBeanFactory) parent) .isPrimary (beanName,beanInstance));}
The getMergedLocalBeanDefinition (beanName) .isPrimary () method corresponds to the primary property of AbstractBeanDefinition, which is assigned in the AnnotatedBeanDefinitionReader.doRegisterBean method. There is the following logic.
/ / A lot of code is omitted. For (Class type) {if (priorityAnnotationType = = null) {return null;} Object cached = priorityCache.get (type); if (cached! = null) {return (cached instanceof Integer? (Integer) cached: null);} Annotation priority = AnnotationUtils.findAnnotation (type, priorityAnnotationType); Integer result = null; if (priority! = null) {result = (Integer) AnnotationUtils.getValue (priority);} priorityCache.put (type, (result! = null? Result: NOT_ANNOTATED); return result;}
Look up on OrderUtils and find that the value of priorityAnnotationType is:
PriorityAnnotationType = (Class
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.