spring DefaultListableBeanFactory 实现了基本的IOC容器
一 查看spring BeanFactory接口源码。
1 String FACTORY_BEAN_PREFIX = "&"; //定义工厂bean的名字前缀为“&”;
2 Object getBean(String name) throws BeansException;//通过名称获取bean,名称可以是别名,也可以是规范名;返回单例或者原型,如果为单例,其他持有者可能修改单例的属性,如果没有这个名称抛出异常;猜想:实现类需要有存储别名的地方。
3 <T> T getBean(String name, Class<T> requiredType) throws BeansException;
4 <T> T getBean(Class<T> requiredType) throws BeansException;//如果有多个bean,或者没有bean抛出异常。
5 Object getBean(String name, Object... args) throws BeansException;//如果bean不是原型抛出异常
5 <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;
6 boolean containsBean(String name);//是否包含bean的名称
7 boolean isSingleton(String name) throws NoSuchBeanDefinitionException;//是否是单例
8 boolean isPrototype(String name) throws NoSuchBeanDefinitionException;//是否是原型
9 boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;//类型是否匹配
10 Class<?> getType(String name) throws NoSuchBeanDefinitionException;//获取bean的类型
11 String[] getAliases(String name);//获取名称的别名
二 BeanFactory的子接口
1 HierarchicalBeanFactory //层次级的BeanFactory
1) BeanFactory getParentBeanFactory();//获取父工厂;猜想:实现类存储有父工厂
2)boolean containsLocalBean(String name);//当前的bean工厂是否具有这个bean名称
2 AutowireCapableBeanFactory;//可自动装配的beanFactory
1)定义了几个装配类型:不装配,按名称,按类型,按构造函数,自动(已过时)
2) <T> T createBean(Class<T> beanClass) throws BeansException;//创建bean,包括bean的后处理器处理
3) void autowireBean(Object existingBean) throws BeansException;//装配一个已存在的bean
4)Object configureBean(Object existingBean, String beanName) throws BeansException;
5) Object resolveDependency(DependencyDescriptor descriptor, String beanName) throws BeansException;//解析bean的依赖描述
6)Object createBean(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;//创建bean
7) Object autowire(Class<?> beanClass, int autowireMode, boolean dependencyCheck) throws BeansException;
8) void autowireBeanProperties(Object existingBean, int autowireMode, boolean dependencyCheck) throws BeansException;//自动装配bean的属性
9)void applyBeanPropertyValues(Object existingBean, String beanName) throws BeansException;//应用bean的属性值
10)Object initializeBean(Object existingBean, String beanName) throws BeansException;//初始化bean
11) Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException;
12) Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException;
13) void destroyBean(Object existingBean);
14) Object resolveDependency(DependencyDescriptor descriptor, String beanName, Set<String> autowiredBeanNames, TypeConverter typeConverter) throws BeansException;
3 ListableBeanFactory;//可列表的beanFactory
1)boolean containsBeanDefinition(String beanName);
2)int getBeanDefinitionCount();
3)String[] getBeanDefinitionNames();
4)String[] getBeanNamesForType(Class<?> type);
5)String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit);
6) <T> Map<String, T> getBeansOfType(Class<T> type) throws BeansException;
7) <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit)
throws BeansException;
8)String[] getBeanNamesForAnnotation(Class<? extends Annotation> annotationType);
9) <T> Map<String, T> getBeansOfType(Class<T> type, boolean includeNonSingletons, boolean allowEagerInit) throws BeansException;
10)Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;
11)<A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) throws NoSuchBeanDefinitionException;
4. ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry ;猜想: 单例bean注册,存储了单例,但是原型的每次都获取
主要是配置beanFactory
设置父工厂,设置类加载器,设置临时类加载器,设置是否缓存元数据,设置bean的表达式解析器,设置转换服务,增加属性编辑注册器,增加用户的属性编辑器,复制属性编辑注册器中的属性编辑器,设置类型转换器
设置字符串表达式解析器,增加bean的后处理器,查看后处理器的数量,注册bean的作用域,获取注册的bean的作用域的数量,按照作用域名称获取java类,获取java的安全控制器,从其他工厂复制配置,注册别名,按照给定的String解析器
解析别名,获取合并的bean定义,判断是否为工厂bean,设置一个bean是否在创建状态,判断一个bean是否在创建状态,注册依赖bean,获取bean的依赖,获取依赖与bean的beans,销毁bean,销毁单例bean,销毁特殊作用域的bean
IOC容器之DefaultListableBeanFactory
原文:https://www.cnblogs.com/jtfBlogs/p/7799424.html