/**
* Extension of the {@link org.springframework.beans.factory.BeanFactory} 扩展自 BeanFactory 的街口,实现了能够凭借 bean 工厂去自动注入 bean 实现
* interface to be implemented by bean factories that are capable of 前提是他们希望为现有的 bean 实例公开此功能
* autowiring, provided that they want to expose this functionality for
* existing bean instances.
*
* <p>This subinterface of BeanFactory is not meant to be used in normal 这个 BeanFactory 的子接口不是通常意义的 applicaiton 代码
* application code: stick to {@link org.springframework.beans.factory.BeanFactory} 这个典型的需求可以使用 BeanFactory 或 BeanFactory
* or {@link org.springframework.beans.factory.ListableBeanFactory} for
* typical use cases.
*
* <p>Integration code for other frameworks can leverage this interface to 其他框架的源码能够影响这个接口去注入和填充 Spring 生命周期不能控制的 bean 实例,
* wire and populate existing bean instances that Spring does not control
* the lifecycle of. This is particularly useful for WebWork Actions and 尤其对于 WebWork 和 Tapestry 尤其有用
* Tapestry Page objects, for example.
*
* <p>Note that this interface is not implemented by 注意这个接口未被 ApplicationContext 直接实现
* {@link org.springframework.context.ApplicationContext} facades,
* as it is hardly ever used by application code. That said, it is available 因为它很少被 application 使用
* from an application context too, accessible through ApplicationContext's 尽管如此,他也可以从 application context 中去获得
* {@link org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()} 可以通过 ApplicationContext#getAutowireCapableBeanFactory() 去获得
* method.
*
* <p>You may also implement the {@link org.springframework.beans.factory.BeanFactoryAware} 你可能实现了 BeanFactoryAware 接口
* interface, which exposes the internal BeanFactory even when running in an 即使在 ApplicationContext 中运行也会暴露内部的 BeanFactory
* ApplicationContext, to get access to an AutowireCapableBeanFactory: 要访问 AutowireCapableBeanFactory:
* simply cast the passed-in BeanFactory to AutowireCapableBeanFactory. 只需要将传入的 BeanFactory 强转为 BeanFactory 即可
*
* @author Juergen Hoeller
* @since 04.12.2003
* @see org.springframework.beans.factory.BeanFactoryAware
* @see org.springframework.beans.factory.config.ConfigurableListableBeanFactory
* @see org.springframework.context.ApplicationContext#getAutowireCapableBeanFactory()
*/
public interface AutowireCapableBeanFactory extends BeanFactory {
/**
* Constant that indicates no externally defined autowiring. Note that
* BeanFactoryAware etc and annotation-driven injection will still be applied.
* @see #createBean
* @see #autowire
* @see #autowireBeanProperties
*/
int AUTOWIRE_NO = 0;
/**
* Constant that indicates autowiring bean properties by name
* (applying to all bean property setters).
* @see #createBean
* @see #autowire
* @see #autowireBeanProperties
*/
int AUTOWIRE_BY_NAME = 1;
/**
* Constant that indicates autowiring bean properties by type
* (applying to all bean property setters).
* @see #createBean
* @see #autowire
* @see #autowireBeanProperties
*/
int AUTOWIRE_BY_TYPE = 2;
/**
* Constant that indicates autowiring the greediest constructor that
* can be satisfied (involves resolving the appropriate constructor).
* @see #createBean
* @see #autowire
*/
int AUTOWIRE_CONSTRUCTOR = 3;
/**
* Constant that indicates determining an appropriate autowire strategy
* through introspection of the bean class.
* @see #createBean
* @see #autowire
* @deprecated as of Spring 3.0: If you are using mixed autowiring strategies,
* prefer annotation-based autowiring for clearer demarcation of autowiring needs.
*/
@Deprecated
int AUTOWIRE_AUTODETECT = 4;
/**
* Suffix for the "original instance" convention when initializing an existing
* bean instance: to be appended to the fully-qualified bean class name,
* e.g. "com.mypackage.MyClass.ORIGINAL", in order to enforce the given instance
* to be returned, i.e. no proxies etc.
* @since 5.1
* @see #initializeBean(Object, String)
* @see #applyBeanPostProcessorsBeforeInitialization(Object, String)
* @see #applyBeanPostProcessorsAfterInitialization(Object, String)
*/
String ORIGINAL_INSTANCE_SUFFIX = ".ORIGINAL";
//-------------------------------------------------------------------------
// Typical methods for creating and populating external bean instances
//-------------------------------------------------------------------------
/**
* Fully create a new bean instance of the given class. 给给定的类创造一个 bean 实例
* <p>Performs full initialization of the bean, including all applicable 履行 bean 的完整初始化,包括所有可用的 BeanPostProcessors
* {@link BeanPostProcessor BeanPostProcessors}.
* <p>Note: This is intended for creating a fresh instance, populating annotated 注意:这是为了创建一个新的实例,填充被注解的字段和方法
* fields and methods as well as applying all standard bean initialization callbacks. 除此之外去请求所有正常 bean 的初始化回调
* It does <i>not</i> imply traditional by-name or by-type autowiring of properties; 它不意味着传统的凭借 名字或类型去注入属性
* use {@link #createBean(Class, int, boolean)} for those purposes. 使用 createBean 实现这些目的
* @param beanClass the class of the bean to create
* @return the new bean instance
* @throws BeansException if instantiation or wiring failed
*/
<T> T createBean(Class<T> beanClass) throws BeansException;
/**
* Populate the given bean instance through applying after-instantiation callbacks 通过在完成 bean 的实例化之后去填充 bean , 以及执行 post-processing(例如注解驱动注入)
* and bean property post-processing (e.g. for annotation-driven injection).
* <p>Note: This is essentially intended for (re-)populating annotated fields and 注意,这本质上是为了填充内部被注解的属性和方法,为了新实例或被反序列化的实例
* methods, either for new instances or for deserialized instances. It does
* <i>not</i> imply traditional by-name or by-type autowiring of properties; 不意味着按照名称或类型注入属性
* use {@link #autowireBeanProperties} for those purposes.
* @param existingBean the existing bean instance
* @throws BeansException if wiring failed
*/
void autowireBean(Object existingBean) throws BeansException;
/**
* Configure the given raw bean: autowiring bean properties, applying 配置给定的原始 bean,自动注入 bean 属性,请求 bean 的属性值
* bean property values, applying factory callbacks such as {@code setBeanName} 请求工厂的回调,例如 setBeanName 和 setBeanFactory
* and {@code setBeanFactory}, and also applying all bean post processors 并且请求所有的 bean post processors
* (including ones which might wrap the given raw bean). 包括给原始 bean 包装
* <p>This is effectively a superset of what {@link #initializeBean} provides,
* fully applying the configuration specified by the corresponding bean definition.
* <b>Note: This method requires a bean definition for the given name!</b>
* @param existingBean the existing bean instance
* @param beanName the name of the bean, to be passed to it if necessary
* (a bean definition of that name has to be available)
* @return the bean instance to use, either the original or a wrapped one
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if there is no bean definition with the given name
* @throws BeansException if the initialization failed
* @see #initializeBean
*/
Object configureBean(Object existingBean, String beanName) throws BeansException;
//-------------------------------------------------------------------------
// Specialized methods for fine-grained control over the bean lifecycle
//-------------------------------------------------------------------------
/**
* Fully create a new bean instance of the given class with the specified 对于指定的自动装配策略去创建一个完整的新的 bean 实例
* autowire strategy. All constants defined in this interface are supported here. 在这支持这个接口中定义的所有常量
* <p>Performs full initialization of the bean, including all applicable 执行这个 bean 的完整初始化,包括所有 BeanPostProcessors
* {@link BeanPostProcessor BeanPostProcessors}. This is effectively a superset 这实际上是 autowire 的超集,提供了 initializeBean 的行为
* of what {@link #autowire} provides, adding {@link #initializeBean} behavior.
* @param beanClass the class of the bean to create
* @param autowireMode by name or type, using the constants in this interface 自动注入的类型,使用这个接口的常量
* @param dependencyCheck whether to perform a dependency check for objects 是否对对象执行依赖项检查
* (not applicable to autowiring a constructor, thus ignored there)
* @return the new bean instance
* @throws BeansException if instantiation or wiring failed
* @see #AUTOWIRE_NO
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_CONSTRUCTOR
*/
Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;
/**
* Instantiate a new bean instance of the given class with the specified autowire 对与给定的 class 与 指定的注入策略,创建一个新的 bean 实例
* strategy. All constants defined in this interface are supported here. 在这支持这个接口中定义的所有常量
* Can also be invoked with {@code AUTOWIRE_NO} in order to just apply 也可以实现非自动注入以提供在 实例化之前的回调(例如注解注入)
* before-instantiation callbacks (e.g. for annotation-driven injection).
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors} 不应用标准的 BeanPostProcessors 回调,也不执行进一步的 bean 初始化
* callbacks or perform any further initialization of the bean. This interface
* offers distinct, fine-grained operations for those purposes, for example 这个接口是独特的,用于细粒度的操作
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor} 例如 initializeBean,然而,
* callbacks are applied, if applicable to the construction of the instance. 如果执行实例的构造。将执行 InstantiationAwareBeanPostProcessor 回调
* @param beanClass the class of the bean to instantiate
* @param autowireMode by name or type, using the constants in this interface
* @param dependencyCheck whether to perform a dependency check for object
* references in the bean instance (not applicable to autowiring a constructor,
* thus ignored there)
* @return the new bean instance
* @throws BeansException if instantiation or wiring failed
* @see #AUTOWIRE_NO
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_CONSTRUCTOR
* @see #AUTOWIRE_AUTODETECT
* @see #initializeBean
* @see #applyBeanPostProcessorsBeforeInitialization
* @see #applyBeanPostProcessorsAfterInitialization
*/
Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;
/**
* Autowire the bean properties of the given bean instance by name or type. 凭借名字或类型自动注入给定 bean 实例的 bean 属性
* Can also be invoked with {@code AUTOWIRE_NO} in order to just apply 也可以实现非自动注入以提供在 实例化之前的回调(例如注解注入)
* after-instantiation callbacks (e.g. for annotation-driven injection).
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors}
* callbacks or perform any further initialization of the bean. This interface 这个接口是独特的,用于细粒度的操作
* offers distinct, fine-grained operations for those purposes, for example
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor} 例如 initializeBean,然而,
* callbacks are applied, if applicable to the configuration of the instance. 如果执行实例的构造。将执行 InstantiationAwareBeanPostProcessor 回调
* @param existingBean the existing bean instance
* @param autowireMode by name or type, using the constants in this interface
* @param dependencyCheck whether to perform a dependency check for object
* references in the bean instance
* @throws BeansException if wiring failed
* @see #AUTOWIRE_BY_NAME
* @see #AUTOWIRE_BY_TYPE
* @see #AUTOWIRE_NO
*/
void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck)
throws BeansException;
/**
* Apply the property values of the bean definition with the given name to 将具有指定名字的 bean definition 的属性值给与给定的 bean 实例
* the given bean instance. The bean definition can either define a fully 这个 bean definition 也可以包含自身
* self-contained bean, reusing its property values, or just property values 重用其属性值,也可以仅定义用于现有bean实例的属性值
* meant to be used for existing bean instances.
* <p>This method does <i>not</i> autowire bean properties; it just applies 这个方法不提供自动注入 bean 属性,这仅适用于显式定义的属性值
* explicitly defined property values. Use the {@link #autowireBeanProperties} 使用 autowireBeanProperties 方法能够为存在的 bean 实例自动注入
* method to autowire an existing bean instance.
* <b>Note: This method requires a bean definition for the given name!</b> 注意:这个方法为给定的名字请求一个 bean definition
* <p>Does <i>not</i> apply standard {@link BeanPostProcessor BeanPostProcessors} 不请求标准的 BeanPostProcessors 回调
* callbacks or perform any further initialization of the bean. This interface 或者更进一步的初始化 bean。
* offers distinct, fine-grained operations for those purposes, for example 这个接口是独特的,用于细粒度的操作
* {@link #initializeBean}. However, {@link InstantiationAwareBeanPostProcessor} 例如 initializeBean,然而,
* callbacks are applied, if applicable to the configuration of the instance. 如果执行实例的构造。将执行 InstantiationAwareBeanPostProcessor 回调
* @param existingBean the existing bean instance
* @param beanName the name of the bean definition in the bean factory
* (a bean definition of that name has to be available)
* @throws org.springframework.beans.factory.NoSuchBeanDefinitionException
* if there is no bean definition with the given name
* @throws BeansException if applying the property values failed
* @see #autowireBeanProperties
*/
void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException;
/**
* Initialize the given raw bean, applying factory callbacks 初始化给定的原始 bean,执行工厂回调
* such as {@code setBeanName} and {@code setBeanFactory}, 例如,setBeanName 和 setBeanFactory
* also applying all bean post processors (including ones which 也执行所有的 bean post processors,
* might wrap the given raw bean). 包括包装给定的原生 bean
* <p>Note that no bean definition of the given name has to exist 注意,给定的 name 可能在 bean factory 中不存在 bean definition
* in the bean factory. The passed-in bean name will simply be used 只使用传入的 bean name 进行回调,但是不对注册的 bean definitions 进行检查
* for callbacks but not checked against the registered bean definitions.
* @param existingBean the existing bean instance
* @param beanName the name of the bean, to be passed to it if necessary bean 的 name,如果需要的话传入
* (only passed to {@link BeanPostProcessor BeanPostProcessors}; (仅传入给 BeanPostProcessors)
* can follow the {@link #ORIGINAL_INSTANCE_SUFFIX} convention in order to 能够通过 ORIGINAL_INSTANCE_SUFFIX(原始 bean 后缀)
* enforce the given instance to be returned, i.e. no proxies etc) 去强制返回给定的实例,
* @return the bean instance to use, either the original or a wrapped one
* @throws BeansException if the initialization failed
* @see #ORIGINAL_INSTANCE_SUFFIX
*/
Object initializeBean(Object existingBean, String beanName) throws BeansException;
/**
* Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean 对于给定的 bean 实例请求 BeanPostProcessors
* instance, invoking their {@code postProcessBeforeInitialization} methods. 执行他们的 postProcessBeforeInitialization 方法
* The returned bean instance may be a wrapper around the original. 返回的类型可能是原始类型的包装器
* @param existingBean the existing bean instance
* @param beanName the name of the bean, to be passed to it if necessary
* (only passed to {@link BeanPostProcessor BeanPostProcessors};
* can follow the {@link #ORIGINAL_INSTANCE_SUFFIX} convention in order to
* enforce the given instance to be returned, i.e. no proxies etc)
* @return the bean instance to use, either the original or a wrapped one 正在使用的 bean 实例,或者原始类型的包装器
* @throws BeansException if any post-processing failed
* @see BeanPostProcessor#postProcessBeforeInitialization
* @see #ORIGINAL_INSTANCE_SUFFIX
*/
Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException;
/**
* Apply {@link BeanPostProcessor BeanPostProcessors} to the given existing bean 对于给定的 bean 实例去执行 BeanPostProcessors
* instance, invoking their {@code postProcessAfterInitialization} methods. 执行他们的 postProcessAfterInitialization 方法
* The returned bean instance may be a wrapper around the original. 返回的类型可能是原始类型的包装器
* @param existingBean the existing bean instance
* @param beanName the name of the bean, to be passed to it if necessary
* (only passed to {@link BeanPostProcessor BeanPostProcessors};
* can follow the {@link #ORIGINAL_INSTANCE_SUFFIX} convention in order to
* enforce the given instance to be returned, i.e. no proxies etc)
* @return the bean instance to use, either the original or a wrapped one
* @throws BeansException if any post-processing failed
* @see BeanPostProcessor#postProcessAfterInitialization
* @see #ORIGINAL_INSTANCE_SUFFIX
*/
Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException;
/**
* Destroy the given bean instance (typically coming from {@link #createBean}),
* applying the {@link org.springframework.beans.factory.DisposableBean} contract as well as
* registered {@link DestructionAwareBeanPostProcessor DestructionAwareBeanPostProcessors}.
* <p>Any exception that arises during destruction should be caught
* and logged instead of propagated to the caller of this method.
* @param existingBean the bean instance to destroy
*/
void destroyBean(Object existingBean);
//-------------------------------------------------------------------------
// Delegate methods for resolving injection points
//-------------------------------------------------------------------------
/**
* Resolve the bean instance that uniquely matches the given object type, if any, 解析唯一匹配对象类型的 bean 实例,如果有的话
* including its bean name. 包括他的 bean name
* <p>This is effectively a variant of {@link #getBean(Class)} which preserves the
* bean name of the matching instance. 这实际上是 getBean 的变形,保留了匹配实例的 bean name
* @param requiredType type the bean must match; can be an interface or superclass
* @return the bean name plus bean instance bean 实例加上 bean name
* @throws NoSuchBeanDefinitionException if no matching bean was found
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
* @throws BeansException if the bean could not be created
* @since 4.3.3
* @see #getBean(Class)
*/
<T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType) throws BeansException;
/**
* Resolve a bean instance for the given bean name, providing a dependency descriptor 解析给定 bean name 的 bean 实例,
* for exposure to target factory methods. 为了暴露目标工厂方法提供的依赖描述符号
* <p>This is effectively a variant of {@link #getBean(String, Class)} which supports 这实际上是 getBean(String, Class) 的变形
* factory methods with an {@link org.springframework.beans.factory.InjectionPoint} 它带有 InjectionPoint 参数的工厂方法
* argument.
* @param name the name of the bean to look up
* @param descriptor the dependency descriptor for the requesting injection point
* @return the corresponding bean instance
* @throws NoSuchBeanDefinitionException if there is no bean with the specified name
* @throws BeansException if the bean could not be created
* @since 5.1.5
* @see #getBean(String, Class)
*/
Object resolveBeanByName(String name, DependencyDescriptor descriptor) throws BeansException;
/**
* Resolve the specified dependency against the beans defined in this factory.
* @param descriptor the descriptor for the dependency (field/method/constructor)
* @param requestingBeanName the name of the bean which declares the given dependency
* @return the resolved object, or {@code null} if none found
* @throws NoSuchBeanDefinitionException if no matching bean was found
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
* @throws BeansException if dependency resolution failed for any other reason
* @since 2.5
* @see #resolveDependency(DependencyDescriptor, String, Set, TypeConverter)
*/
@Nullable
Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName) throws BeansException;
/**
* Resolve the specified dependency against the beans defined in this factory.
* @param descriptor the descriptor for the dependency (field/method/constructor)
* @param requestingBeanName the name of the bean which declares the given dependency
* @param autowiredBeanNames a Set that all names of autowired beans (used for
* resolving the given dependency) are supposed to be added to
* @param typeConverter the TypeConverter to use for populating arrays and collections
* @return the resolved object, or {@code null} if none found
* @throws NoSuchBeanDefinitionException if no matching bean was found
* @throws NoUniqueBeanDefinitionException if more than one matching bean was found
* @throws BeansException if dependency resolution failed for any other reason
* @since 2.5
* @see DependencyDescriptor
*/
@Nullable
Object resolveDependency(DependencyDescriptor descriptor, @Nullable String requestingBeanName,
@Nullable Set<String> autowiredBeanNames, @Nullable TypeConverter typeConverter) throws BeansException;
}
主要方法
方法名 | 含义 |
---|---|
T createBean(Class beanClass) | 履行 bean 的完整初始化,包括所有可用的 BeanPostProcessors |
void autowireBean(Object existingBean) | 通过在完成 bean 的实例化之后去填充 bean , 以及执行 post-processing |
Object configureBean(Object existingBean, String beanName) | 配置给定的原始 bean,自动注入 bean 属性,请求 bean 的属性值请求工厂的回调,例如 setBeanName 和 setBeanFactory |
Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) | 对于指定的自动装配策略去创建一个完整的新的 bean 实例 |
Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) | 对与给定的 class 与 指定的注入策略,创建一个新的 bean 实例 |
void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) | 凭借名字或类型自动注入给定 bean 实例的 bean 属性 |