Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Application of Spring automatic injection

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the application of Spring automatic injection". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the application of Spring automatic injection.

Automatic assembly @ Nullablepublic Object doResolveDependency (DependencyDescriptor descriptor, @ Nullable String beanName, @ Nullable Set autowiredBeanNames, @ Nullable TypeConverter typeConverter) throws BeansException {InjectionPoint previousInjectionPoint = ConstructorResolver.setCurrentInjectionPoint (descriptor); try {Object shortcut = descriptor.resolveShortcut (this) If (shortcut! = null) {return shortcut;} / / class com.ypf.ServiceB Class type = descriptor.getDependencyType (); Object value = getAutowireCandidateResolver () .getSuggestedValue (descriptor) If (value! = null) {if (value instanceof String) {String strVal = resolveEmbeddedValue ((String) value); BeanDefinition bd = (beanName! = null & & containsBean (beanName)? GetMergedBeanDefinition (beanName): null); value = evaluateBeanDefinitionString (strVal, bd);} TypeConverter converter = (typeConverter! = null? TypeConverter: getTypeConverter (); return (descriptor.getField ()! = null? Converter.convertIfNecessary (value, type, descriptor.getField ()): converter.convertIfNecessary (value, type, descriptor.getMethodParameter ());} Object multipleBeans = resolveMultipleBeans (descriptor, beanName, autowiredBeanNames, typeConverter) If (multipleBeans! = null) {return multipleBeans;} / / key points! Go to the spring ioc container to find the type type bean Map matchingBeans = findAutowireCandidates (beanName, type, descriptor) to be injected; if (matchingBeans.isEmpty ()) {if (isRequired (descriptor)) {raiseNoMatchingBeanFound (type, descriptor.getResolvableType (), descriptor) } return null;} String autowiredBeanName; Object instanceCandidate; if (matchingBeans.size () > 1) {/ / Determine the autowire candidate in the given set of beans. / / Looks for {@ code @ Primary} and {@ code @ Priority} (in that order). AutowiredBeanName = determineAutowireCandidate (matchingBeans, descriptor); if (autowiredBeanName = = null) {if (isRequired (descriptor) | |! indicatesMultipleBeans (type)) {return descriptor.resolveNotUnique (type, matchingBeans) } else {/ / In case of an optional Collection/Map Silently ignore a non-unique case: / / possibly it was meant to bean empty collection of multiple regular beans / / (before 4.3 in particular when we didn't even look for collection beans). Return null;}} instanceCandidate = matchingBeans.get (autowiredBeanName);} else {/ / We have exactly one match. / / extract the resulting bean from the map of matchingBeans. Map.Entry entry = matchingBeans.entrySet (). Iterator (). Next (); autowiredBeanName = entry.getKey (); instanceCandidate = entry.getValue () } / / Why add a successful injection of beanName into a Set collection if (autowiredBeanNames! = null) {autowiredBeanNames.add (autowiredBeanName);} / /? If (instanceCandidate instanceof Class) {instanceCandidate = descriptor.resolveCandidate (autowiredBeanName, type, this);} Object result = instanceCandidate If (result instanceof NullBean) {if (isRequired (descriptor)) {raiseNoMatchingBeanFound (type, descriptor.getResolvableType (), descriptor);} result = null } if (! ClassUtils.isAssignableValue (type, result)) {throw new BeanNotOfRequiredTypeException (autowiredBeanName, type, instanceCandidate.getClass ());} return result } finally {ConstructorResolver.setCurrentInjectionPoint (previousInjectionPoint);}} DependencyDescriptor

Depending on the descriptor, it roughly describes that the field in XXX needs to automatically assemble bean of type xxx.

GetBean is actually called in descriptor.resolveCandidate.

FindAutowireCandidates

Find some candidateNames according to some rules, put these candidateName,beanInstance into map and return. BeanInstance is obtained by calling getBean (candidateName).

Protected Map findAutowireCandidates (@ Nullable String beanName, Class requiredType, DependencyDescriptor descriptor) {/ / first find all the name of this type of bean in the spring ioc container according to the type of Class that is automatically assembled. / / Ancestors ancestor String [] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors (this, requiredType, true, descriptor.isEager ()); Map result = new LinkedHashMap (candidateNames.length) / / parsed dependencies will be cached. According to requiredType, there are no dependencies in the cache. If so, for (Class autowiringType: this.resolvableDependencies.keySet ()) {if (autowiringType.isAssignableFrom (requiredType)) {Object autowiringValue = this.resolvableDependencies.get (autowiringType) is directly fetched. AutowiringValue = AutowireUtils.resolveAutowiringValue (autowiringValue, requiredType); if (requiredType.isInstance (autowiringValue)) {result.put (ObjectUtils.identityToString (autowiringValue), autowiringValue); break } for (String candidate: candidateNames) {/ / if the type of auto-assembly property is not this class and is a candidate for auto-assembly, it is added to result. Press byType if (! isSelfReference (beanName, candidate) & & isAutowireCandidate (candidate, descriptor)) {addCandidateEntry (result, candidate, descriptor, requiredType) }} if (result.isEmpty () & &! indicatesMultipleBeans (requiredType)) {/ / Consider fallback matches if the first pass failed to find anything... DependencyDescriptor fallbackDescriptor = descriptor.forFallbackMatch (); for (String candidate: candidateNames) {/ / add to result if the auto-assembly attribute is of a type other than this class and is a candidate for auto-assembly. Press byName to find it. If (! isSelfReference (beanName, candidate) & & isAutowireCandidate (candidate, fallbackDescriptor)) {addCandidateEntry (result, candidate, descriptor, requiredType);}} if (result.isEmpty ()) {/ / Consider self references as a final pass... / / but in the case of a dependency collection, not the very same bean itself. For (String candidate: candidateNames) {if (isSelfReference (beanName, candidate) & & (! (descriptor instanceof MultiElementDescriptor) | |! beanName.equals (candidate)) & & isAutowireCandidate (candidate FallbackDescriptor) {addCandidateEntry (result, candidate, descriptor, requiredType) } return result;}

???

IsAutowireCandidate

Press byType to find it, and then press byName to find it?

Protected boolean isAutowireCandidate (String beanName, DependencyDescriptor descriptor, AutowireCandidateResolver resolver) throws NoSuchBeanDefinitionException {String beanDefinitionName = BeanFactoryUtils.transformedBeanName (beanName); if (containsBeanDefinition (beanDefinitionName)) {return isAutowireCandidate (beanName, getMergedLocalBeanDefinition (beanDefinitionName), descriptor, resolver) } else if (containsSingleton (beanName)) {return isAutowireCandidate (beanName, new RootBeanDefinition (getType (beanName)), descriptor, resolver);} BeanFactory parent = getParentBeanFactory (); if (parent instanceof DefaultListableBeanFactory) {/ / No bean definition found in this factory-> delegate to parent. Return ((DefaultListableBeanFactory) parent) .isAutowireCandidate (beanName, descriptor, resolver);} else if (parent instanceof ConfigurableListableBeanFactory) {/ / If no DefaultListableBeanFactory, can't pass the resolver along. Return ((ConfigurableListableBeanFactory) parent) .isAutowireCandidate (beanName, descriptor);} else {return true }} Thank you for your reading, the above is the content of "the application of Spring automatic injection". After the study of this article, I believe you have a deeper understanding of the application of Spring automatic injection, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report