001/* 002 * Copyright 2002-2020 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.beans.factory.support; 018 019import org.springframework.beans.BeanUtils; 020import org.springframework.beans.factory.config.BeanDefinitionHolder; 021import org.springframework.beans.factory.config.DependencyDescriptor; 022import org.springframework.lang.Nullable; 023 024/** 025 * Strategy interface for determining whether a specific bean definition 026 * qualifies as an autowire candidate for a specific dependency. 027 * 028 * @author Juergen Hoeller 029 * @author Mark Fisher 030 * @since 2.5 031 */ 032public interface AutowireCandidateResolver { 033 034 /** 035 * Determine whether the given bean definition qualifies as an 036 * autowire candidate for the given dependency. 037 * <p>The default implementation checks 038 * {@link org.springframework.beans.factory.config.BeanDefinition#isAutowireCandidate()}. 039 * @param bdHolder the bean definition including bean name and aliases 040 * @param descriptor the descriptor for the target method parameter or field 041 * @return whether the bean definition qualifies as autowire candidate 042 * @see org.springframework.beans.factory.config.BeanDefinition#isAutowireCandidate() 043 */ 044 default boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { 045 return bdHolder.getBeanDefinition().isAutowireCandidate(); 046 } 047 048 /** 049 * Determine whether the given descriptor is effectively required. 050 * <p>The default implementation checks {@link DependencyDescriptor#isRequired()}. 051 * @param descriptor the descriptor for the target method parameter or field 052 * @return whether the descriptor is marked as required or possibly indicating 053 * non-required status some other way (e.g. through a parameter annotation) 054 * @since 5.0 055 * @see DependencyDescriptor#isRequired() 056 */ 057 default boolean isRequired(DependencyDescriptor descriptor) { 058 return descriptor.isRequired(); 059 } 060 061 /** 062 * Determine whether the given descriptor declares a qualifier beyond the type 063 * (typically - but not necessarily - a specific kind of annotation). 064 * <p>The default implementation returns {@code false}. 065 * @param descriptor the descriptor for the target method parameter or field 066 * @return whether the descriptor declares a qualifier, narrowing the candidate 067 * status beyond the type match 068 * @since 5.1 069 * @see org.springframework.beans.factory.annotation.QualifierAnnotationAutowireCandidateResolver#hasQualifier 070 */ 071 default boolean hasQualifier(DependencyDescriptor descriptor) { 072 return false; 073 } 074 075 /** 076 * Determine whether a default value is suggested for the given dependency. 077 * <p>The default implementation simply returns {@code null}. 078 * @param descriptor the descriptor for the target method parameter or field 079 * @return the value suggested (typically an expression String), 080 * or {@code null} if none found 081 * @since 3.0 082 */ 083 @Nullable 084 default Object getSuggestedValue(DependencyDescriptor descriptor) { 085 return null; 086 } 087 088 /** 089 * Build a proxy for lazy resolution of the actual dependency target, 090 * if demanded by the injection point. 091 * <p>The default implementation simply returns {@code null}. 092 * @param descriptor the descriptor for the target method parameter or field 093 * @param beanName the name of the bean that contains the injection point 094 * @return the lazy resolution proxy for the actual dependency target, 095 * or {@code null} if straight resolution is to be performed 096 * @since 4.0 097 */ 098 @Nullable 099 default Object getLazyResolutionProxyIfNecessary(DependencyDescriptor descriptor, @Nullable String beanName) { 100 return null; 101 } 102 103 /** 104 * Return a clone of this resolver instance if necessary, retaining its local 105 * configuration and allowing for the cloned instance to get associated with 106 * a new bean factory, or this original instance if there is no such state. 107 * <p>The default implementation creates a separate instance via the default 108 * class constructor, assuming no specific configuration state to copy. 109 * Subclasses may override this with custom configuration state handling 110 * or with standard {@link Cloneable} support (as implemented by Spring's 111 * own configurable {@code AutowireCandidateResolver} variants), or simply 112 * return {@code this} (as in {@link SimpleAutowireCandidateResolver}). 113 * @since 5.2.7 114 * @see GenericTypeAwareAutowireCandidateResolver#cloneIfNecessary() 115 * @see DefaultListableBeanFactory#copyConfigurationFrom 116 */ 117 default AutowireCandidateResolver cloneIfNecessary() { 118 return BeanUtils.instantiateClass(getClass()); 119 } 120 121}