1751 2017-12-02 2024-07-28
DefaultListableBeanFactory类被称为Spring的发动机/导火索,这次我们先从类层次结构上来了解下。
重点看下接口层面方法,具体的实现类先不细究(为了简化阅读,只保留了基础的方法,去掉大部分注释)。
一、类结构
DefaultListableBeanFactory是Spring中最为核心的几个类之一。它的类声明如下
/**
* Default implementation of the
* {@link org.springframework.beans.factory.ListableBeanFactory} and
* {@link BeanDefinitionRegistry} interfaces: a full-fledged bean factory
* based on bean definition objects.
*
* <p>Typical usage is registering all bean definitions first (possibly read
* from a bean definition file), before accessing beans. Bean definition lookup
* is therefore an inexpensive operation in a local bean definition table,
* operating on pre-built bean definition metadata objects.
*
* <p>Can be used as a standalone bean factory, or as a superclass for custom
* bean factories. Note that readers for specific bean definition formats are
* typically implemented separately rather than as bean factory subclasses:
* see for example {@link PropertiesBeanDefinitionReader} and
* {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
*
* <p>For an alternative implementation of the
* {@link org.springframework.beans.factory.ListableBeanFactory} interface,
* have a look at {@link StaticListableBeanFactory}, which manages existing
* bean instances rather than creating new ones based on bean definitions.
*
*/
// 这里简单翻译下还是很有必要的,大致意思如下:DefaultListableBeanFactory是一个成熟的基于BeanDefinition对象的BeanFactory。在内存中操作BeanDefinition元数据是很容易的,难的是从外部文件读取BeanDefinition。需要注意,从外部读取BeanDefinitions的工作是委托个外部做的,子类也不需要管。该类只提供一个注册BeanDefinition的方法:registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
// 省略其他代码
}
DefaultListableBeanFactory是整个bean加载的核心部分,是Spring注册及加载bean的默认实现。
DefaultListableBeanFactory 继承了 AbstractAutowireCapableBeanFactory,并实现了 ConfigurableListableBeanFactory以及 BeanDefinitionRegistry接口。以下是DefaultListableBeanFactory的类图。
从上面的类图中,我们可以很清晰地从全局角度了解DefaultListableBeanFactory的脉络。先简单地了解一下上面类图中的各个类的作用,如下(自上而下,自左向右)
- AliasRegistry(接口):定义对alias(别名)的简单增删改等操作。
- BeanDefinitionRegistry(接口):定义对BeanDefinition的各种增删改操作。
- SimpleAliasRegistry:主要使用map作为alias的缓存,并对接口AliasRegistry进行实现。
- SingletonBeanRegistry(接口):定义对单例的注册及获取。
- BeanFactory(接口):定义获取bean及bean的各种属性。
- DefaultSingletonBeanRegistry:对接口SingletonBeanRegistry各函数的实现。
- HierarchicaBeanFactory(接口):继承BeanFactory,也就是在BeanFactory定义的功能的基础上增加对parentFactory的支持。
- ListableBeanFactory(接口):根据各种条件获取bean的配置清单。
- FactoryBeanRegistrySupport:在DefaultListableBeanFactory的基础上,增加对FactoryBean的特殊处理功能。
- ConfigurableBeanFactory(接口):提供配置Factory的各种方法。
- AbstractBeanFactory:综合FactoryBeanRegistrySupport和ConfigurableBeanFactory的功能。
- AutowireCapableBeanFactory(接口):提供创建Bean、自动注入、初始化以及应用bean的后处理器。
- AbstractAutowireCapableBeanFactory:综合AbstractBeanFactory并对接口AutowireCapableBeanFactory进行实现。
- ConfigurableListableBeanFactory(接口):BeanFactory配置清单,指定忽略类型及接口等。
- DefaultListableBeanFactory:综合上面所有功能,主要是对Bean注册后的处理。
下面我们针对最上层的几个接口进行说明,请自行阅读相关注释(第一次看不懂很正常,后面回过头来多看看就好了)。
二、上层接口
接口是对事物特性的封装展现,因此我们先看下DefaultListableBeanFactory所实现的接口的具体定义。
1、AliasRegistry
别名的注册管理器接口,代码如下
public interface AliasRegistry {
void registerAlias(String name, String alias);
void removeAlias(String alias);
boolean isAlias(String beanName);
String[] getAliases(String name);
}
2、BeanDefinitionRegistry
BeanDefinition的注册管理器接口,代码如下
public interface BeanDefinitionRegistry extends AliasRegistry {
void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException;
void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
boolean containsBeanDefinition(String beanName);
String[] getBeanDefinitionNames();
int getBeanDefinitionCount();
boolean isBeanNameInUse(String beanName);
}
3、BeanFactory
管理bean的最顶层工厂接口,代码如下
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
Object getBean(String name) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
boolean containsBean(String name);
boolean isSingleton(String name) throws NoSuchBeanDefinitionException;
boolean isPrototype(String name) throws NoSuchBeanDefinitionException;
boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
String[] getAliases(String name);
}
4、SingletonBeanRegistry
单例bean的注册管理器接口,代码如下
public interface SingletonBeanRegistry {
void registerSingleton(String beanName, Object singletonObject);
Object getSingleton(String beanName);
boolean containsSingleton(String beanName);
String[] getSingletonNames();
int getSingletonCount();
}
5、HierarchicalBeanFactory
分层的bean工厂接口,代码如下
public interface HierarchicalBeanFactory extends BeanFactory {
BeanFactory getParentBeanFactory();
boolean containsLocalBean(String name);
}
6、ListableBeanFactory
可列举的bean工厂接口,定义了大量获取bean的方法,代码如下
public interface ListableBeanFactory extends BeanFactory {
boolean containsBeanDefinition(String beanName);
int getBeanDefinitionCount();
String[] getBeanDefinitionNames();
String[] getBeanNamesForType(Class<?> type);
String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);
<T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;
<T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
throws BeansException;
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
throws BeansException;
<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType);
}
7、ConfigurableBeanFactory
配置bean工厂的接口,提供配置bean工厂的各种方法,代码如下
public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {
String SCOPE_SINGLETON = "singleton";
String SCOPE_PROTOTYPE = "prototype";
void setParentBeanFactory(BeanFactory parentBeanFactory) throws IllegalStateException;
void setBeanClassLoader(ClassLoader beanClassLoader);
ClassLoader getBeanClassLoader();
void setTempClassLoader(ClassLoader tempClassLoader);
ClassLoader getTempClassLoader();
void setCacheBeanMetadata(boolean cacheBeanMetadata);
boolean isCacheBeanMetadata();
void setBeanExpressionResolver(BeanExpressionResolver resolver);
BeanExpressionResolver getBeanExpressionResolver();
void setConversionService(ConversionService conversionService);
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);
String resolveEmbeddedValue(String value);
void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);
int getBeanPostProcessorCount();
void registerScope(String scopeName, Scope scope);
String[] getRegisteredScopeNames();
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();
}
8、AutowireCapableBeanFactory
自动装填的bean工厂接口,提供创建bean、自动注入、初始化以及应用bean的后处理器,代码如下
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;
<T> T createBean(Class<T> beanClass) throws BeansException;
void autowireBean(Object existingBean) throws BeansException;
Object configureBean(Object existingBean, String beanName) throws BeansException;
Object resolveDependency(DependencyDescriptor descriptor, 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;
Object resolveDependency(DependencyDescriptor descriptor, String beanName,
Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException;
}
9、ConfigurableListableBeanFactory
可列举的配置bean工厂接口,配置BeanFactory,指定忽略类型及接口等,代码如下
public interface ConfigurableListableBeanFactory
extends ListableBeanFactory, AutowireCapableBeanFactory, ConfigurableBeanFactory {
void ignoreDependencyType(Class<?> type);
void ignoreDependencyInterface(Class<?> ifc);
void registerResolvableDependency(Class<?> dependencyType, Object autowiredValue);
boolean isAutowireCandidate(String beanName, DependencyDescriptor descriptor)
throws NoSuchBeanDefinitionException;
BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
void freezeConfiguration();
boolean isConfigurationFrozen();
void preInstantiateSingletons() throws BeansException;
}
三、上层父类
父类的话,只做简单的类说明,不做过多深究。
1、SimpleAliasRegistry
简单别名注册管理器,代码如下
public class SimpleAliasRegistry implements AliasRegistry {
//省略代码
}
2、DefaultSingletonBeanRegistry
单例bean的默认注册管理器,代码如下
public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
//省略代码
}
3、FactoryBeanRegistrySupport
在DefaultSingletonBeanRegistry的基础上增加了对FactoryBean的特殊处理功能,代码如下
public abstract class FactoryBeanRegistrySupport extends DefaultSingletonBeanRegistry {
//省略代码
}
4、AbstractBeanFactory
综合FactoryBeanRegistry和ConfigurableBeanFactory的功能,代码如下
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {
//省略代码
}
5、AbstractAutowireCapableBeanFactory
综合AbstractBeanFactory并对接口AutowireCapableBeanFactory进行实现,代码如下
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory
implements AutowireCapableBeanFactory {
//省略代码
}
纸上得来终觉浅,绝知此事需躬行。一步一个脚印,终究能到达本来难以登顶的高山!
总访问次数: 22次, 一般般帅 创建于 2017-12-02, 最后更新于 2024-07-28
欢迎关注微信公众号,第一时间掌握最新动态!