【源码】Spring —— BeanFactory 解读 1 bean工厂分支接口
前言版本BeanFactoryHierarchicalBeanFactoryConfigurableBeanFactory
ListableBeanFactoryAutowireCapableBeanFactoryConfigurableListableBeanFactory
类图总结
前言
本系列章节解读 BeanFactory 接口,这是 Spring 最核心的接口之一,其下有两大分支
bean工厂:管理 Spring容器 中所有的 bean,实现 Spring 的核心功能 —— 依赖注入(Dependency Injection)Application:基于 bean工厂 管理 bean,注册并管理 BeanDefinition,加载并刷新容器配置,笼统意义上的 Spring容器
本章节从 bean工厂 开始解读,先介绍所有相关的接口
版本
Spring 5.2.x
BeanFactory
public interface BeanFactory {
String FACTORY_BEAN_PREFIX
= "&";
Object
getBean(String name
) throws BeansException
;
<T> T
getBean(String name
, Class
<T> requiredType
) throws BeansException
;
Object
getBean(String name
, Object
... args
) throws BeansException
;
<T> T
getBean(Class
<T> requiredType
) throws BeansException
;
<T> T
getBean(Class
<T> requiredType
, Object
... args
) throws BeansException
;
<T> ObjectProvider
<T> getBeanProvider(Class
<T> requiredType
);
<T> ObjectProvider
<T> getBeanProvider(ResolvableType requiredType
);
boolean containsBean(String name
);
boolean isSingleton(String name
) throws NoSuchBeanDefinitionException
;
boolean isPrototype(String name
) throws NoSuchBeanDefinitionException
;
boolean isTypeMatch(String name
, ResolvableType typeToMatch
) throws NoSuchBeanDefinitionException
;
boolean isTypeMatch(String name
, Class
<?> typeToMatch
) throws NoSuchBeanDefinitionException
;
@Nullable
Class
<?> getType(String name
) throws NoSuchBeanDefinitionException
;
@Nullable
Class
<?> getType(String name
, boolean allowFactoryBeanInit
) throws NoSuchBeanDefinitionException
;
String
[] getAliases(String name
);
}
提供了以上方法,其中包括各种 getBean 方法。其中 ResolvableType 类可以理解为 Spring 对 JDK Type 类的封装,之后的类也会经常用到
关于 Type 和 ResolvableType,可以阅读下面文章
关于 Type 和 ResolvableType
HierarchicalBeanFactory
public interface HierarchicalBeanFactory extends BeanFactory {
@Nullable
BeanFactory
getParentBeanFactory();
boolean containsLocalBean(String name
);
}
拓展维护了 层级 关系,getParentBeanFactory 方法可获取父级 bean工厂
ConfigurableBeanFactory
public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry
{
String SCOPE_SINGLETON
= "singleton";
String SCOPE_PROTOTYPE
= "prototype";
void setParentBeanFactory(BeanFactory parentBeanFactory
) throws IllegalStateException
;
void setBeanClassLoader(@Nullable ClassLoader beanClassLoader
);
@Nullable
ClassLoader
getBeanClassLoader();
void setTempClassLoader(@Nullable ClassLoader tempClassLoader
);
@Nullable
ClassLoader
getTempClassLoader();
void setCacheBeanMetadata(boolean cacheBeanMetadata
);
boolean isCacheBeanMetadata();
void setBeanExpressionResolver(@Nullable BeanExpressionResolver resolver
);
@Nullable
BeanExpressionResolver
getBeanExpressionResolver();
void setConversionService(@Nullable ConversionService conversionService
);
@Nullable
ConversionService
getConversionService();
void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar
);
void registerCustomEditor(Class
<?> requiredType
, Class
<? extends PropertyEditor> propertyEditorClass
);
void copyRegisteredEditorsTo(PropertyEditorRegistry registry
);
void setTypeConverter(TypeConverter typeConverter
);
TypeConverter
getTypeConverter();
void addEmbeddedValueResolver(StringValueResolver valueResolver
);
boolean hasEmbeddedValueResolver();
@Nullable
String
resolveEmbeddedValue(String value
);
void addBeanPostProcessor(BeanPostProcessor beanPostProcessor
);
int getBeanPostProcessorCount();
void registerScope(String scopeName
, Scope scope
);
String
[] getRegisteredScopeNames();
@Nullable
Scope
getRegisteredScope(String scopeName
);
AccessControlContext
getAccessControlContext();
void copyConfigurationFrom(ConfigurableBeanFactory otherFactory
);
void registerAlias(String beanName
, String alias
) throws BeanDefinitionStoreException
;
void resolveAliases(StringValueResolver valueResolver
);
BeanDefinition
getMergedBeanDefinition(String beanName
) throws NoSuchBeanDefinitionException
;
boolean isFactoryBean(String name
) throws NoSuchBeanDefinitionException
;
void setCurrentlyInCreation(String beanName
, boolean inCreation
);
boolean isCurrentlyInCreation(String beanName
);
void registerDependentBean(String beanName
, String dependentBeanName
);
String
[] getDependentBeans(String beanName
);
String
[] getDependenciesForBean(String beanName
);
void destroyBean(String beanName
, Object beanInstance
);
void destroyScopedBean(String beanName
);
void destroySingletons();
}
在 HierarchicalBeanFactory 的基础上拓展了各种配置能力,比如 ClassLoader ConversionService 等
关于 ConversionService 的介绍,可以阅读下面文章
Spring —— Convert 解读
ListableBeanFactory
public interface ListableBeanFactory extends BeanFactory {
boolean containsBeanDefinition(String beanName
);
int getBeanDefinitionCount();
String
[] getBeanDefinitionNames();
String
[] getBeanNamesForType(ResolvableType type
);
String
[] getBeanNamesForType(ResolvableType type
, boolean includeNonSingletons
, boolean allowEagerInit
);
String
[] getBeanNamesForType(@Nullable Class
<?> type
);
String
[] getBeanNamesForType(@Nullable Class
<?> type
, boolean includeNonSingletons
, boolean allowEagerInit
);
<T> Map
<String, T> getBeansOfType(@Nullable Class
<T> type
) throws BeansException
;
<T> Map
<String, T> getBeansOfType(@Nullable Class
<T> type
, boolean includeNonSingletons
, boolean allowEagerInit
)
throws BeansException
;
String
[] getBeanNamesForAnnotation(Class
<? extends Annotation> annotationType
);
Map
<String, Object> getBeansWithAnnotation(Class
<? extends Annotation> annotationType
) throws BeansException
;
@Nullable
<A
extends Annotation> A
findAnnotationOnBean(String beanName
, Class
<A> annotationType
)
throws NoSuchBeanDefinitionException
;
}
BeanFactory 的拓展,提供了 枚举 所有 bean实例 的功能。
注意,如果同时拓展了 HierarchicalBeanFactory ,并不会枚举其他
层级的 bean实例
AutowireCapableBeanFactory
public interface AutowireCapableBeanFactory extends BeanFactory {
int AUTOWIRE_NO
= 0;
int AUTOWIRE_BY_NAME
= 1;
int AUTOWIRE_BY_TYPE
= 2;
int AUTOWIRE_CONSTRUCTOR
= 3;
@Deprecated
int AUTOWIRE_AUTODETECT
= 4;
String ORIGINAL_INSTANCE_SUFFIX
= ".ORIGINAL";
<T> T
createBean(Class
<T> beanClass
) throws BeansException
;
void autowireBean(Object existingBean
) throws BeansException
;
Object
configureBean(Object existingBean
, String beanName
) throws BeansException
;
Object
createBean(Class
<?> beanClass
, int autowireMode
, boolean dependencyCheck
) throws BeansException
;
Object
autowire(Class
<?> beanClass
, int autowireMode
, boolean dependencyCheck
) throws BeansException
;
void autowireBeanProperties(Object existingBean
, int autowireMode
, boolean dependencyCheck
)
throws BeansException
;
void applyBeanPropertyValues(Object existingBean
, String beanName
) throws BeansException
;
Object
initializeBean(Object existingBean
, String beanName
) throws BeansException
;
Object
applyBeanPostProcessorsBeforeInitialization(Object existingBean
, String beanName
)
throws BeansException
;
Object
applyBeanPostProcessorsAfterInitialization(Object existingBean
, String beanName
)
throws BeansException
;
void destroyBean(Object existingBean
);
<T> NamedBeanHolder
<T> resolveNamedBean(Class
<T> requiredType
) throws BeansException
;
Object
resolveBeanByName(String name
, DependencyDescriptor descriptor
) throws BeansException
;
@Nullable
Object
resolveDependency(DependencyDescriptor descriptor
, @Nullable String requestingBeanName
) throws BeansException
;
@Nullable
Object
resolveDependency(DependencyDescriptor descriptor
, @Nullable String requestingBeanName
,
@Nullable Set
<String> autowiredBeanNames
, @Nullable TypeConverter typeConverter
) throws BeansException
;
}
拓展了 BeanFactory,提供了对bean实例 生命周期 的细粒度控制,比如 属性注入、初始化实例、后处理方法 等
ConfigurableListableBeanFactory
public interface ConfigurableListableBeanFactory
extends ListableBeanFactory, AutowireCapableBeanFactory
, ConfigurableBeanFactory
{
void ignoreDependencyType(Class
<?> type
);
void ignoreDependencyInterface(Class
<?> ifc
);
void registerResolvableDependency(Class
<?> dependencyType
, @Nullable Object autowiredValue
);
boolean isAutowireCandidate(String beanName
, DependencyDescriptor descriptor
)
throws NoSuchBeanDefinitionException
;
BeanDefinition
getBeanDefinition(String beanName
) throws NoSuchBeanDefinitionException
;
Iterator
<String> getBeanNamesIterator();
void clearMetadataCache();
void freezeConfiguration();
boolean isConfigurationFrozen();
void preInstantiateSingletons() throws BeansException
;
}
拓展了 ListableBeanFactory AutowireCapableBeanFactory 和 ConfigurableBeanFactory,是功能最强大的接口
类图
总结
本章节介绍了 BeanFactory 体系下 bean工厂 分支的几个主要接口
BeanFactory :顶层接口HierarchicalBeanFactory :拓展了 层级 的维护ListableBeanFactory :拓展了 枚举 所有 bean实例 的功能AutowireCapableBeanFactory :提供 自动装配 的能力ConfigurableBeanFactory:提供了 配置 的能力ConfigurableListableBeanFactory:在上述所有接口基础上拓展了若干方法
下一章节,我们介绍抽象实现类 AbstractBeanFactory
下一章节:【源码】Spring —— BeanFactory 解读 2 AbstractBeanFactory