Bean容器的始祖BeanFactory
BeanFactory是Spring容器的根接口,具体定义如下:
public interface BeanFactory {
// 工厂Bean的前缀
String FACTORY_BEAN_PREFIX = "&";
// 按名称获取Bean
Object getBean(String name) throws BeansException;
<T> T getBean(String name, Class<T> requiredType) throws BeansException;
Object getBean(String name, Object... args) throws BeansException;
// 按类型获取Bean
<T> T getBean(Class<T> requiredType) throws BeansException;
<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
// 获取ObjectProvider
<T> ObjectProvider<T> getBeanProvider(Class<T> requiredType);
<T> ObjectProvider<T> getBeanProvider(ResolvableType requiredType);
// check方法
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;
// 通过Bean的名称获取类型
Class<?> getType(String name) throws NoSuchBeanDefinitionException;
Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException;
// 获取bean的别名
String[] getAliases(String name);
}
其中各方法的作用已经在代码中注释了。
BeanFactory接口层级结构
我们看到BeanFactory的整体层级结构如下所示,我特意把ApplicationContext去除掉了,这个在后续的文章中单独列一篇文章来分析:
BeanFactory的三个直接子接口分别是:AutowireCapableBeanFactory、HierarchicalBeanFactory、ListableBeanFactory
三个子接口分别提供不同的能力扩展,具体的源代码如下所示:
AutowireCapableBeanFactory扩展Autowire功能
public interface AutowireCapableBeanFactory extends BeanFactory {
// 一些常量定义
int AUTOWIR