/** * Determine whether the provided bean definition is an autowire candidate. * <p>To be considered a candidate the bean's <em>autowire-candidate</em> * attribute must not have been set to 'false'. Also, if an annotation on * the field or parameter to be autowired is recognized by this bean factory * as a <em>qualifier</em>, the bean must 'match' against the annotation as * well as any attributes it may contain. The bean definition must contain * the same qualifier or match by meta attributes. A "value" attribute will * fallback to match against the bean name or an alias if a qualifier or * attribute does not match. * @see Qualifier */ @Override public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { boolean match = super.isAutowireCandidate(bdHolder, descriptor); if (match && descriptor != null) { match = checkQualifiers(bdHolder, descriptor.getAnnotations()); if (match) { MethodParameter methodParam = descriptor.getMethodParameter(); if (methodParam != null) { Method method = methodParam.getMethod(); if (method == null || void.class.equals(method.getReturnType())) { match = checkQualifiers(bdHolder, methodParam.getMethodAnnotations()); } } } } return match; }
protected ResolvableType getReturnTypeForFactoryMethod(RootBeanDefinition rbd, DependencyDescriptor descriptor) { // Should typically be set for any kind of factory method, since the BeanFactory // pre-resolves them before reaching out to the AutowireCandidateResolver... Class<?> preResolved = rbd.resolvedFactoryMethodReturnType; if (preResolved != null) { return ResolvableType.forClass(preResolved); } else { Method resolvedFactoryMethod = rbd.getResolvedFactoryMethod(); if (resolvedFactoryMethod != null) { if (descriptor.getDependencyType().isAssignableFrom(resolvedFactoryMethod.getReturnType())) { // Only use factory method metadata if the return type is actually expressive enough // for our dependency. Otherwise, the returned instance type may have matched instead // in case of a singleton instance having been registered with the container already. return ResolvableType.forMethodReturnType(resolvedFactoryMethod); } } return null; } }
/** * Determine whether the specified bean definition qualifies as an autowire candidate, * to be injected into other beans which declare a dependency of matching type. * @param beanName the name of the bean definition to check * @param descriptor the descriptor of the dependency to resolve * @param resolver the AutowireCandidateResolver to use for the actual resolution algorithm * @return whether the bean should be considered as autowire candidate */ 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); } else if (getParentBeanFactory() instanceof DefaultListableBeanFactory) { // No bean definition found in this factory -> delegate to parent. return ((DefaultListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor, resolver); } else if (getParentBeanFactory() instanceof ConfigurableListableBeanFactory) { // If no DefaultListableBeanFactory, can't pass the resolver along. return ((ConfigurableListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor); } else { return true; } }
/** * Determine whether the specified bean definition qualifies as an autowire candidate, * to be injected into other beans which declare a dependency of matching type. * @param beanName the name of the bean definition to check * @param mbd the merged bean definition to check * @param descriptor the descriptor of the dependency to resolve * @param resolver the AutowireCandidateResolver to use for the actual resolution algorithm * @return whether the bean should be considered as autowire candidate */ protected boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd, DependencyDescriptor descriptor, AutowireCandidateResolver resolver) { String beanDefinitionName = BeanFactoryUtils.transformedBeanName(beanName); resolveBeanClass(mbd, beanDefinitionName); if (mbd.isFactoryMethodUnique) { boolean resolve; synchronized (mbd.constructorArgumentLock) { resolve = (mbd.resolvedConstructorOrFactoryMethod == null); } if (resolve) { new ConstructorResolver(this).resolveFactoryMethodIfPossible(mbd); } } return resolver.isAutowireCandidate( new BeanDefinitionHolder(mbd, beanName, getAliases(beanDefinitionName)), descriptor); }
@Override public Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException { descriptor.initParameterNameDiscovery(getParameterNameDiscoverer()); if (descriptor.getDependencyType().equals(ObjectFactory.class)) { return new DependencyObjectFactory(descriptor, beanName); } else if (descriptor.getDependencyType().equals(javaxInjectProviderClass)) { return new DependencyProviderFactory().createDependencyProvider(descriptor, beanName); } else { Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, beanName); if (result == null) { result = doResolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter); } return result; } }
@Bean @Scope(scopeName = "prototype") @SuppressWarnings("unchecked") public <ITEM> Grid<ITEM> configureBeanGrid(DependencyDescriptor dependencyDescriptor) { logger.debug("Configuring Vaadin Grid as bean"); long timestamp = System.currentTimeMillis(); ResolvableType injectionPointType = dependencyDescriptor.getResolvableType(); if (!injectionPointType.hasGenerics()) { throw new IllegalStateException("Grid injection point is expected to declare a static item type"); } ResolvableType genericType = injectionPointType.getGeneric(); Class<ITEM> itemType = (Class<ITEM>) genericType.resolve(); logger.debug("Vaadin Grid will use " + itemType.getCanonicalName() + " as item type"); Grid<ITEM> grid = configureGridInstance(itemType); long configTime = System.currentTimeMillis() - timestamp; logger.debug("Done configuring Grid for " + itemType.getName() + " in " + configTime + "ms"); return grid; }
/** * Determine whether the provided bean definition is an autowire candidate. * <p>To be considered a candidate the bean's <em>autowire-candidate</em> * attribute must not have been set to 'false'. Also, if an annotation on * the field or parameter to be autowired is recognized by this bean factory * as a <em>qualifier</em>, the bean must 'match' against the annotation as * well as any attributes it may contain. The bean definition must contain * the same qualifier or match by meta attributes. A "value" attribute will * fallback to match against the bean name or an alias if a qualifier or * attribute does not match. * @see Qualifier */ @Override public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { boolean match = super.isAutowireCandidate(bdHolder, descriptor); if (match && descriptor != null) { match = checkQualifiers(bdHolder, descriptor.getAnnotations()); if (match) { MethodParameter methodParam = descriptor.getMethodParameter(); if (methodParam != null) { Method method = methodParam.getMethod(); if (method == null || void.class == method.getReturnType()) { match = checkQualifiers(bdHolder, methodParam.getMethodAnnotations()); } } } } return match; }
@Override public Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException { descriptor.initParameterNameDiscovery(getParameterNameDiscoverer()); if (descriptor.getDependencyType().equals(javaUtilOptionalClass)) { return new OptionalDependencyFactory().createOptionalDependency(descriptor, beanName); } else if (ObjectFactory.class == descriptor.getDependencyType()) { return new DependencyObjectFactory(descriptor, beanName); } else if (javaxInjectProviderClass == descriptor.getDependencyType()) { return new DependencyProviderFactory().createDependencyProvider(descriptor, beanName); } else { Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, beanName); if (result == null) { result = doResolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter); } return result; } }
/** * Determine the autowire candidate in the given set of beans. * <p>Looks for {@code @Primary} and {@code @Priority} (in that order). * @param candidateBeans a Map of candidate names and candidate instances * that match the required type, as returned by {@link #findAutowireCandidates} * @param descriptor the target dependency to match against * @return the name of the autowire candidate, or {@code null} if none found */ protected String determineAutowireCandidate(Map<String, Object> candidateBeans, DependencyDescriptor descriptor) { Class<?> requiredType = descriptor.getDependencyType(); String primaryCandidate = determinePrimaryCandidate(candidateBeans, requiredType); if (primaryCandidate != null) { return primaryCandidate; } String priorityCandidate = determineHighestPriorityCandidate(candidateBeans, requiredType); if (priorityCandidate != null) { return priorityCandidate; } // Fallback for (Map.Entry<String, Object> entry : candidateBeans.entrySet()) { String candidateBeanName = entry.getKey(); Object beanInstance = entry.getValue(); if ((beanInstance != null && this.resolvableDependencies.containsValue(beanInstance)) || matchesBeanName(candidateBeanName, descriptor.getDependencyName())) { return candidateBeanName; } } return null; }
@Ignore @Test public void testAutowireCandidateWithFieldDescriptor() throws Exception { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); lbf.registerBeanDefinition(JUERGEN, person1); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); lbf.registerBeanDefinition(MARK, person2); DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor( QualifiedTestBean.class.getDeclaredField("qualified"), false); DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor( QualifiedTestBean.class.getDeclaredField("nonqualified"), false); assertTrue(lbf.isAutowireCandidate(JUERGEN, null)); assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)); assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); assertTrue(lbf.isAutowireCandidate(MARK, null)); assertTrue(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor)); assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)); }
@Test public void testAutowireCandidateExplicitlyFalseWithFieldDescriptor() throws Exception { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null); person.setAutowireCandidate(false); person.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); lbf.registerBeanDefinition(JUERGEN, person); DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor( QualifiedTestBean.class.getDeclaredField("qualified"), false); DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor( QualifiedTestBean.class.getDeclaredField("nonqualified"), false); assertFalse(lbf.isAutowireCandidate(JUERGEN, null)); assertFalse(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)); assertFalse(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); }
@Test public void testAutowireCandidateWithShortClassName() throws Exception { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); ConstructorArgumentValues cavs = new ConstructorArgumentValues(); cavs.addGenericArgumentValue(JUERGEN); RootBeanDefinition person = new RootBeanDefinition(Person.class, cavs, null); person.addQualifier(new AutowireCandidateQualifier(ClassUtils.getShortName(TestQualifier.class))); lbf.registerBeanDefinition(JUERGEN, person); DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor( QualifiedTestBean.class.getDeclaredField("qualified"), false); DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor( QualifiedTestBean.class.getDeclaredField("nonqualified"), false); assertTrue(lbf.isAutowireCandidate(JUERGEN, null)); assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor)); assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); }
@Ignore @Test public void testAutowireCandidateWithConstructorDescriptor() throws Exception { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); lbf.registerBeanDefinition(JUERGEN, person1); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); lbf.registerBeanDefinition(MARK, person2); MethodParameter param = new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0); DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(param, false); param.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer()); assertEquals("tpb", param.getParameterName()); assertTrue(lbf.isAutowireCandidate(JUERGEN, null)); assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)); }
@Test public void testAutowireCandidateWithMultipleCandidatesDescriptor() throws Exception { DefaultListableBeanFactory lbf = new DefaultListableBeanFactory(); ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); cavs1.addGenericArgumentValue(JUERGEN); RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); lbf.registerBeanDefinition(JUERGEN, person1); ConstructorArgumentValues cavs2 = new ConstructorArgumentValues(); cavs2.addGenericArgumentValue(MARK); RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null); person2.addQualifier(new AutowireCandidateQualifier(TestQualifier.class)); lbf.registerBeanDefinition(MARK, person2); DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor( new MethodParameter(QualifiedTestBean.class.getDeclaredConstructor(Person.class), 0), false); assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor)); assertTrue(lbf.isAutowireCandidate(MARK, qualifiedDescriptor)); }
@Override protected Map<String, Object> findAutowireCandidates(String beanName, Class requiredType, DependencyDescriptor descriptor) { Map<String, Object> tmp= super.findAutowireCandidates(beanName, requiredType,descriptor); Map<String, Object> result= new HashMap<String,Object>(); for(Iterator<Entry<String,Object>> it=tmp.entrySet().iterator();it.hasNext();){ Entry<String,Object> entry= it.next(); String key= entry.getKey(); Object value= entry.getValue(); if(key.indexOf(Constants.FACTORY_SEGMENT)>0){ result.put(key.split(Constants.FACTORY_SEGMENT)[1], value); continue; } result.put(key, value); } return result; }
@Override public Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException { if (descriptor == null || descriptor.getField() == null || !descriptor.getField().getType().equals(Ignite.class)) return super.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter); else { if (configuration == null) configuration = new IgniteSpringBootConfiguration( createBean(DefaultIgniteProperties.class)); return configuration.getIgnite( descriptor.getField().getAnnotationsByType(IgniteResource.class)); } }
@Override public Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException { descriptor.initParameterNameDiscovery(getParameterNameDiscoverer()); if (descriptor.getDependencyType().equals(javaUtilOptionalClass)) { return new OptionalDependencyFactory().createOptionalDependency(descriptor, beanName); } else if (ObjectFactory.class == descriptor.getDependencyType() || ObjectProvider.class == descriptor.getDependencyType()) { return new DependencyObjectProvider(descriptor, beanName); } else if (javaxInjectProviderClass == descriptor.getDependencyType()) { return new Jsr330ProviderFactory().createDependencyProvider(descriptor, beanName); } else { Object result = getAutowireCandidateResolver().getLazyResolutionProxyIfNecessary(descriptor, beanName); if (result == null) { result = doResolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter); // 解析依赖 } return result; } }
@Override public Object getIfUnique() throws BeansException { DependencyDescriptor descriptorToUse = new DependencyDescriptor(descriptor) { @Override public boolean isRequired() { return false; } @Override public Object resolveNotUnique(Class<?> type, Map<String, Object> matchingBeans) { return null; } }; if (this.optional) { return new OptionalDependencyFactory().createOptionalDependency(descriptorToUse, this.beanName); } else { return doResolveDependency(descriptorToUse, this.beanName, null, null); } }
@Override public Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException { Field field = descriptor.getField(); if (field != null && Logger.class == descriptor.getDependencyType()) { return LoggerFactory.getLogger(getDeclaringClass(descriptor)); } if (field != null && Config.class.isAssignableFrom(field.getType())) { return getConfig(field.getType()); } MethodParameter methodParam = descriptor.getMethodParameter(); if (methodParam != null && Config.class.isAssignableFrom(methodParam.getParameterType())) { return getConfig(methodParam.getParameterType()); } return super.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter); }
public boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor) throws NoSuchBeanDefinitionException { // Consider FactoryBeans as autowiring candidates. boolean isFactoryBean = (descriptor != null && descriptor.getDependencyType() != null && FactoryBean.class.isAssignableFrom(descriptor.getDependencyType())); if (isFactoryBean) { beanName = BeanFactoryUtils.transformedBeanName(beanName); } if (containsBeanDefinition(beanName)) { return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanName), descriptor); } else if (containsSingleton(beanName)) { return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor); } else if (getParentBeanFactory() instanceof ConfigurableListableBeanFactory) { // No bean definition found in this factory -> delegate to parent. return ((ConfigurableListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor); } else { return true; } }
/** * Determine whether the specified bean definition qualifies as an autowire candidate, * to be injected into other beans which declare a dependency of matching type. * @param beanName the name of the bean definition to check * @param mbd the merged bean definition to check * @param descriptor the descriptor of the dependency to resolve * @return whether the bean should be considered as autowire candidate */ protected boolean isAutowireCandidate(String beanName, RootBeanDefinition mbd, DependencyDescriptor descriptor) { resolveBeanClass(mbd, beanName); // Rice : removing code due to package restrictions, not used in dictionary // if (mbd.isFactoryMethodUnique) { // boolean resolve; // synchronized (mbd.constructorArgumentLock) { // resolve = (mbd.resolvedConstructorOrFactoryMethod == null); // } // if (resolve) { // new ConstructorResolver(this).resolveFactoryMethodIfPossible(mbd); // } // } // Rice : end code removal return getAutowireCandidateResolver().isAutowireCandidate( new BeanDefinitionHolder(mbd, beanName, getAliases(beanName)), descriptor); }
/** * Find bean instances that match the required type. * Called during autowiring for the specified bean. * @param beanName the name of the bean that is about to be wired * @param requiredType the actual type of bean to look for * (may be an array component type or collection element type) * @param descriptor the descriptor of the dependency to resolve * @return a Map of candidate names and candidate instances that match * the required type (never {@code null}) * @throws BeansException in case of errors * @see #autowireByType * @see #autowireConstructor */ protected Map<String, Object> findAutowireCandidates( String beanName, Class<?> requiredType, DependencyDescriptor descriptor) { String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this, requiredType, true, descriptor.isEager()); Map<String, Object> result = new LinkedHashMap<String, Object>(candidateNames.length); for (Class<?> autowiringType : this.resolvableDependencies.keySet()) { if (autowiringType.isAssignableFrom(requiredType)) { Object autowiringValue = this.resolvableDependencies.get(autowiringType); autowiringValue = resolveAutowiringValue(autowiringValue, requiredType); if (requiredType.isInstance(autowiringValue)) { result.put(ObjectUtils.identityToString(autowiringValue), autowiringValue); break; } } } for (String candidateName : candidateNames) { if (!candidateName.equals(beanName) && isAutowireCandidate(candidateName, descriptor)) { result.put(candidateName, getBean(candidateName)); } } return result; }
@SuppressWarnings({ "unchecked", "rawtypes" }) private Object getInstanceAdapterFor(DependencyDescriptor descriptor) { ListableBeanFactory listableBeanFactory = (ListableBeanFactory) getBeanFactory(); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) listableBeanFactory; // Instance<TargetType> Class targetType = descriptor.getResolvableType().getGeneric(0).getRawClass(); Map<String, Object> beansOfType = listableBeanFactory.getBeansOfType(targetType); List<Bean> beansInstances = beansOfType.entrySet().stream() // .map(e -> new Bean(e.getValue(), registry.getBeanDefinition(e.getKey()).isPrimary()))// .collect(Collectors.toList()); Annotation[] qualifiers = retainQualifierAnnotations(descriptor.getAnnotations()); Beans beans = new Beans(targetType, beansInstances); return qualifiers.length == 0 ? beans : beans.select(qualifiers); }
/** * Determine whether the provided bean definition is an autowire candidate. * <p>To be considered a candidate the bean's <em>autowire-candidate</em> * attribute must not have been set to 'false'. Also, if an annotation on * the field or parameter to be autowired is recognized by this bean factory * as a <em>qualifier</em>, the bean must 'match' against the annotation as * well as any attributes it may contain. The bean definition must contain * the same qualifier or match by meta attributes. A "value" attribute will * fallback to match against the bean name or an alias if a qualifier or * attribute does not match. * @see Qualifier */ public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { if (!bdHolder.getBeanDefinition().isAutowireCandidate()) { // if explicitly false, do not proceed with qualifier check return false; } if (descriptor == null) { // no qualification necessary return true; } boolean match = checkQualifiers(bdHolder, descriptor.getAnnotations()); if (match) { MethodParameter methodParam = descriptor.getMethodParameter(); if (methodParam != null) { Method method = methodParam.getMethod(); if (method == null || void.class.equals(method.getReturnType())) { match = checkQualifiers(bdHolder, methodParam.getMethodAnnotations()); } } } return match; }
/** * Find bean instances that match the required type. * Called during autowiring for the specified bean. * @param beanName the name of the bean that is about to be wired * @param requiredType the actual type of bean to look for * (may be an array component type or collection element type) * @param descriptor the descriptor of the dependency to resolve * @return a Map of candidate names and candidate instances that match * the required type (never {@code null}) * @throws BeansException in case of errors * @see #autowireByType * @see #autowireConstructor */ protected Map<String, Object> findAutowireCandidates( String beanName, Class<?> requiredType, DependencyDescriptor descriptor) { String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( this, requiredType, true, descriptor.isEager()); Map<String, Object> result = new LinkedHashMap<String, Object>(candidateNames.length); for (Class<?> autowiringType : this.resolvableDependencies.keySet()) { if (autowiringType.isAssignableFrom(requiredType)) { Object autowiringValue = this.resolvableDependencies.get(autowiringType); autowiringValue = AutowireUtils.resolveAutowiringValue(autowiringValue, requiredType); if (requiredType.isInstance(autowiringValue)) { result.put(ObjectUtils.identityToString(autowiringValue), autowiringValue); break; } } } for (String candidateName : candidateNames) { if (!candidateName.equals(beanName) && isAutowireCandidate(candidateName, descriptor)) { result.put(candidateName, getBean(candidateName)); } } return result; }
@Override public boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor) throws NoSuchBeanDefinitionException { // Consider FactoryBeans as autowiring candidates. boolean isFactoryBean = (descriptor != null && descriptor.getDependencyType() != null && FactoryBean.class.isAssignableFrom(descriptor.getDependencyType())); if (isFactoryBean) { beanName = BeanFactoryUtils.transformedBeanName(beanName); } if (containsBeanDefinition(beanName)) { return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanName), descriptor); } else if (containsSingleton(beanName)) { return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor); } else if (getParentBeanFactory() instanceof ConfigurableListableBeanFactory) { // No bean definition found in this factory -> delegate to parent. return ((ConfigurableListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor); } else { return true; } }