configurablebeanfactory

喜欢ヅ旅行 2023-08-17 16:04 159阅读 0赞

在这里插入图片描述
ConfigurableBeanFactory定义BeanFactory的配置.ConfigurableBeanFactory中定义了太多太多的api,比如类加载器,类型转化,属性编辑器,BeanPostProcessor,作用域,bean定义,处理bean依赖关系,合并其他ConfigurableBeanFactory,bean如何销毁.

ConfigurableBeanFactory同时继承了HierarchicalBeanFactory 和 SingletonBeanRegistry 这两个接口,即同时继承了分层和单例类注册的功能。

具体:

1、2个静态不可变常量分别代表单例类和原型类。

2、1个设置父工厂的方法,跟HierarchicalBeanFactory接口的getParentBeanFactory方法互补。

3、4个跟类加载器有关的方法:get/set工厂类加载器和get/set临时类加载器。

4、2个设置、是否缓存元数据的方法(热加载开关)。

5、11个处理Bean注册、加载等细节的方法,包括:Bean表达式分解器、转换服务、属性编辑登记员、属性编辑器、属性编辑注册器、类型转换器、嵌入式的字符串分解器

6、2个处理Bean后处理器的方法。

7、3个跟注册范围相关的方法。

8、1个返回安全访问上下文的方法、1个从其他的工厂复制相关的所有配置的方法。

9、2个跟Bean别名相关的方法、1个返回合并后的Bean定义的方法。

10、1个判断是否为工厂Bean的方法、2个跟当前Bean创建时机相关的方法。

11、3个跟Bean依赖相关的方法、3个销毁Bean相关的方法。

总结:这个巨大的工厂接口,继承自HierarchicalBeanFactory 和 SingletonBeanRegistry 这两个接口,并额外独有37个方法!!!(看的我都快疯了…)这37个方法包含了工厂创建、注册一个Bean的众多细节。这个工厂名为ConfigurableBeanFactory,真是名不虚传!统计一下此时的ConfigurableBeanFactory的方法数吧。自有的37个方法、HierarchicalBeanFactory的2个方法、SingletonBeanRegistry的5个方法、爷爷接口BeanFactory的10个方法,共有54个方法!虽然方法繁多,还算井井有条!

  1. /** * Configuration interface to be implemented by most bean factories. Provides * facilities to configure a bean factory, in addition to the bean factory * client methods in the {@link org.springframework.beans.factory.BeanFactory} * interface. * * <p>This bean factory interface is not meant to be used in normal application * code: Stick to {@link org.springframework.beans.factory.BeanFactory} or * {@link org.springframework.beans.factory.ListableBeanFactory} for typical * needs. This extended interface is just meant to allow for framework-internal * plug'n'play and for special access to bean factory configuration methods. * * @author Juergen Hoeller * @since 03.11.2003 * @see org.springframework.beans.factory.BeanFactory * @see org.springframework.beans.factory.ListableBeanFactory * @see ConfigurableListableBeanFactory */
  2. /** * 定义BeanFactory的配置. * * 这边定义了太多太多的api,比如类加载器,类型转化,属性编辑器,BeanPostProcessor,作用域,bean定义,处理bean依赖关系,合并其他ConfigurableBeanFactory,bean如何销毁. * * @author DemoTransfer * @since 4.3 */
  3. public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {
  4. //-------------------------------------------------------------------------
  5. // 定义了两个作用域: 单例和原型.可以通过registerScope来添加.
  6. // SCOPE_SINGLETON,SCOPE_PROTOTYPE
  7. //-------------------------------------------------------------------------
  8. /** * Scope identifier for the standard singleton scope: "singleton". * Custom scopes can be added via {@code registerScope}. * @see #registerScope */
  9. // 单例
  10. String SCOPE_SINGLETON = "singleton";
  11. /** * Scope identifier for the standard prototype scope: "prototype". * Custom scopes can be added via {@code registerScope}. * @see #registerScope */
  12. // 原型
  13. String SCOPE_PROTOTYPE = "prototype";
  14. /** * Set the parent of this bean factory. * <p>Note that the parent cannot be changed: It should only be set outside * a constructor if it isn't available at the time of factory instantiation. * @param parentBeanFactory the parent BeanFactory * @throws IllegalStateException if this factory is already associated with * a parent BeanFactory * @see #getParentBeanFactory() */
  15. // 父容器设置.而且一旦设置了就不让修改
  16. /* * 搭配HierarchicalBeanFactory接口的getParentBeanFactory方法 */
  17. void setParentBeanFactory(BeanFactory parentBeanFactory) throws IllegalStateException;
  18. /** * Set the class loader to use for loading bean classes. * Default is the thread context class loader. * <p>Note that this class loader will only apply to bean definitions * that do not carry a resolved bean class yet. This is the case as of * Spring 2.0 by default: Bean definitions only carry bean class names, * to be resolved once the factory processes the bean definition. * @param beanClassLoader the class loader to use, * or {@code null} to suggest the default class loader */
  19. // 类加载器设置与获取.默认使用当前线程中的类加载器
  20. /* * 设置、返回工厂的类加载器 */
  21. void setBeanClassLoader(ClassLoader beanClassLoader);
  22. /** * Return this factory's class loader for loading bean classes. */
  23. // 类加载器设置与获取.默认使用当前线程中的类加载器
  24. ClassLoader getBeanClassLoader();
  25. /** * Specify a temporary ClassLoader to use for type matching purposes. * Default is none, simply using the standard bean ClassLoader. * <p>A temporary ClassLoader is usually just specified if * <i>load-time weaving</i> is involved, to make sure that actual bean * classes are loaded as lazily as possible. The temporary loader is * then removed once the BeanFactory completes its bootstrap phase. * @since 2.5 */
  26. // 为了类型匹配,搞个临时类加载器.好在一般情况为null,使用上面定义的标准加载器
  27. /* * 设置、返回一个临时的类加载器 */
  28. void setTempClassLoader(ClassLoader tempClassLoader);
  29. /** * Return the temporary ClassLoader to use for type matching purposes, * if any. * @since 2.5 */
  30. // 为了类型匹配,搞个临时类加载器.好在一般情况为null,使用上面定义的标准加载器
  31. ClassLoader getTempClassLoader();
  32. /** * Set whether to cache bean metadata such as given bean definitions * (in merged fashion) and resolved bean classes. Default is on. * <p>Turn this flag off to enable hot-refreshing of bean definition objects * and in particular bean classes. If this flag is off, any creation of a bean * instance will re-query the bean class loader for newly resolved classes. */
  33. // 是否需要缓存bean metadata,比如bean difinition 和 解析好的classes.默认开启缓存
  34. /* * 设置、是否缓存元数据,如果false,那么每次请求实例,都会从类加载器重新加载(热加载) */
  35. void setCacheBeanMetadata(boolean cacheBeanMetadata);
  36. /** * Return whether to cache bean metadata such as given bean definitions * (in merged fashion) and resolved bean classes. */
  37. // 是否需要缓存bean metadata,比如bean difinition 和 解析好的classes.默认开启缓存
  38. // 是否缓存元数据
  39. boolean isCacheBeanMetadata();
  40. /** * Specify the resolution strategy for expressions in bean definition values. * <p>There is no expression support active in a BeanFactory by default. * An ApplicationContext will typically set a standard expression strategy * here, supporting "#{...}" expressions in a Unified EL compatible style. * @since 3.0 */
  41. // 定义用于解析bean definition的表达式解析器
  42. /* * Bean表达式分解器 */
  43. void setBeanExpressionResolver(BeanExpressionResolver resolver);
  44. /** * Return the resolution strategy for expressions in bean definition values. * @since 3.0 */
  45. // 定义用于解析bean definition的表达式解析器
  46. BeanExpressionResolver getBeanExpressionResolver();
  47. /** * Specify a Spring 3.0 ConversionService to use for converting * property values, as an alternative to JavaBeans PropertyEditors. * @since 3.0 */
  48. // 类型转化器
  49. /* * 设置、返回一个转换服务 */
  50. void setConversionService(ConversionService conversionService);
  51. /** * Return the associated ConversionService, if any. * @since 3.0 */
  52. // 类型转化器
  53. ConversionService getConversionService();
  54. /** * Add a PropertyEditorRegistrar to be applied to all bean creation processes. * <p>Such a registrar creates new PropertyEditor instances and registers them * on the given registry, fresh for each bean creation attempt. This avoids * the need for synchronization on custom editors; hence, it is generally * preferable to use this method instead of {@link #registerCustomEditor}. * @param registrar the PropertyEditorRegistrar to register */
  55. // 属性编辑器
  56. /* * 设置属性编辑登记员... */
  57. void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar);
  58. /** * Register the given custom property editor for all properties of the * given type. To be invoked during factory configuration. * <p>Note that this method will register a shared custom editor instance; * access to that instance will be synchronized for thread-safety. It is * generally preferable to use {@link #addPropertyEditorRegistrar} instead * of this method, to avoid for the need for synchronization on custom editors. * @param requiredType type of the property * @param propertyEditorClass the {@link PropertyEditor} class to register */
  59. // 属性编辑器
  60. /* * 注册常用属性编辑器 */
  61. void registerCustomEditor(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass);
  62. /** * Initialize the given PropertyEditorRegistry with the custom editors * that have been registered with this BeanFactory. * @param registry the PropertyEditorRegistry to initialize */
  63. // 属性编辑器
  64. /* * 用工厂中注册的通用的编辑器初始化指定的属性编辑注册器 */
  65. void copyRegisteredEditorsTo(PropertyEditorRegistry registry);
  66. /** * Set a custom type converter that this BeanFactory should use for converting * bean property values, constructor argument values, etc. * <p>This will override the default PropertyEditor mechanism and hence make * any custom editors or custom editor registrars irrelevant. * @see #addPropertyEditorRegistrar * @see #registerCustomEditor * @since 2.5 */
  67. // BeanFactory用来转换bean属性值或者参数值的自定义转换器
  68. /* * 设置、得到一个类型转换器 */
  69. void setTypeConverter(TypeConverter typeConverter);
  70. /** * Obtain a type converter as used by this BeanFactory. This may be a fresh * instance for each call, since TypeConverters are usually <i>not</i> thread-safe. * <p>If the default PropertyEditor mechanism is active, the returned * TypeConverter will be aware of all custom editors that have been registered. * @since 2.5 */
  71. // BeanFactory用来转换bean属性值或者参数值的自定义转换器
  72. TypeConverter getTypeConverter();
  73. /** * Add a String resolver for embedded values such as annotation attributes. * @param valueResolver the String resolver to apply to embedded values * @since 3.0 */
  74. // string值解析器(想起mvc中的ArgumentResolver了)
  75. /* * 增加一个嵌入式的StringValueResolver */
  76. void addEmbeddedValueResolver(StringValueResolver valueResolver);
  77. /** * Determine whether an embedded value resolver has been registered with this * bean factory, to be applied through {@link #resolveEmbeddedValue(String)}. * @since 4.3 */
  78. boolean hasEmbeddedValueResolver();
  79. /** * Resolve the given embedded value, e.g. an annotation attribute. * @param value the value to resolve * @return the resolved value (may be the original value as-is) * @since 3.0 */
  80. // string值解析器(想起mvc中的ArgumentResolver了)
  81. //分解指定的嵌入式的值
  82. String resolveEmbeddedValue(String value);
  83. /** * Add a new BeanPostProcessor that will get applied to beans created * by this factory. To be invoked during factory configuration. * <p>Note: Post-processors submitted here will be applied in the order of * registration; any ordering semantics expressed through implementing the * {@link org.springframework.core.Ordered} interface will be ignored. Note * that autodetected post-processors (e.g. as beans in an ApplicationContext) * will always be applied after programmatically registered ones. * @param beanPostProcessor the post-processor to register */
  84. // BeanPostProcessor用于增强bean初始化功能
  85. //设置一个Bean后处理器
  86. void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);
  87. /** * Return the current number of registered BeanPostProcessors, if any. */
  88. //返回Bean后处理器的数量
  89. int getBeanPostProcessorCount();
  90. /** * Register the given scope, backed by the given Scope implementation. * @param scopeName the scope identifier * @param scope the backing Scope implementation */
  91. // 作用域定义
  92. //注册范围
  93. void registerScope(String scopeName, Scope scope);
  94. /** * Return the names of all currently registered scopes. * <p>This will only return the names of explicitly registered scopes. * Built-in scopes such as "singleton" and "prototype" won't be exposed. * @return the array of scope names, or an empty array if none * @see #registerScope */
  95. // 作用域定义
  96. //返回注册的范围名
  97. String[] getRegisteredScopeNames();
  98. /** * Return the Scope implementation for the given scope name, if any. * <p>This will only return explicitly registered scopes. * Built-in scopes such as "singleton" and "prototype" won't be exposed. * @param scopeName the name of the scope * @return the registered Scope implementation, or {@code null} if none * @see #registerScope */
  99. // 作用域定义
  100. //返回指定的范围
  101. Scope getRegisteredScope(String scopeName);
  102. /** * Provides a security access control context relevant to this factory. * @return the applicable AccessControlContext (never {@code null}) * @since 3.0 */
  103. // 访问权限控制
  104. //返回本工厂的一个安全访问上下文
  105. AccessControlContext getAccessControlContext();
  106. /** * Copy all relevant configuration from the given other factory. * <p>Should include all standard configuration settings as well as * BeanPostProcessors, Scopes, and factory-specific internal settings. * Should not include any metadata of actual bean definitions, * such as BeanDefinition objects and bean name aliases. * @param otherFactory the other BeanFactory to copy from */
  107. // 合并其他ConfigurableBeanFactory的配置,包括上面说到的BeanPostProcessor,作用域等
  108. //从其他的工厂复制相关的所有配置
  109. void copyConfigurationFrom(ConfigurableBeanFactory otherFactory);
  110. /** * Given a bean name, create an alias. We typically use this method to * support names that are illegal within XML ids (used for bean names). * <p>Typically invoked during factory configuration, but can also be * used for runtime registration of aliases. Therefore, a factory * implementation should synchronize alias access. * @param beanName the canonical name of the target bean * @param alias the alias to be registered for the bean * @throws BeanDefinitionStoreException if the alias is already in use */
  111. // bean定义处理
  112. // 注册别名
  113. /* * 给指定的Bean注册别名 */
  114. void registerAlias(String beanName, String alias) throws BeanDefinitionStoreException;
  115. /** * Resolve all alias target names and aliases registered in this * factory, applying the given StringValueResolver to them. * <p>The value resolver may for example resolve placeholders * in target bean names and even in alias names. * @param valueResolver the StringValueResolver to apply * @since 2.5 */
  116. // bean定义处理
  117. //根据指定的StringValueResolver移除所有的别名
  118. void resolveAliases(StringValueResolver valueResolver);
  119. /** * Return a merged BeanDefinition for the given bean name, * merging a child bean definition with its parent if necessary. * Considers bean definitions in ancestor factories as well. * @param beanName the name of the bean to retrieve the merged definition for * @return a (potentially merged) BeanDefinition for the given bean * @throws NoSuchBeanDefinitionException if there is no bean definition with the given name * @since 2.5 */
  120. // bean定义处理
  121. // 合并bean定义,包括父容器的
  122. /* * 返回指定Bean合并后的Bean定义 */
  123. BeanDefinition getMergedBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
  124. /** * Determine whether the bean with the given name is a FactoryBean. * @param name the name of the bean to check * @return whether the bean is a FactoryBean * ({@code false} means the bean exists but is not a FactoryBean) * @throws NoSuchBeanDefinitionException if there is no bean with the given name * @since 2.5 */
  125. // bean定义处理
  126. // 是否是FactoryBean类型
  127. //判断指定Bean是否为一个工厂Bean
  128. boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException;
  129. /** * Explicitly control the current in-creation status of the specified bean. * For container-internal use only. * @param beanName the name of the bean * @param inCreation whether the bean is currently in creation * @since 3.1 */
  130. // bean创建状态控制.在解决循环依赖时有使用
  131. //设置一个Bean是否正在创建
  132. void setCurrentlyInCreation(String beanName, boolean inCreation);
  133. /** * Determine whether the specified bean is currently in creation. * @param beanName the name of the bean * @return whether the bean is currently in creation * @since 2.5 */
  134. // bean创建状态控制.在解决循环依赖时有使用
  135. //返回指定Bean是否已经成功创建
  136. boolean isCurrentlyInCreation(String beanName);
  137. /** * Register a dependent bean for the given bean, * to be destroyed before the given bean is destroyed. * @param beanName the name of the bean * @param dependentBeanName the name of the dependent bean * @since 2.5 */
  138. // 处理bean依赖问题
  139. //注册一个依赖于指定bean的Bean
  140. void registerDependentBean(String beanName, String dependentBeanName);
  141. /** * Return the names of all beans which depend on the specified bean, if any. * @param beanName the name of the bean * @return the array of dependent bean names, or an empty array if none * @since 2.5 */
  142. // 处理bean依赖问题
  143. //返回依赖于指定Bean的所欲Bean名
  144. String[] getDependentBeans(String beanName);
  145. /** * Return the names of all beans that the specified bean depends on, if any. * @param beanName the name of the bean * @return the array of names of beans which the bean depends on, * or an empty array if none * @since 2.5 */
  146. // 处理bean依赖问题
  147. //返回指定Bean依赖的所有Bean名
  148. String[] getDependenciesForBean(String beanName);
  149. /** * Destroy the given bean instance (usually a prototype instance * obtained from this factory) according to its bean definition. * <p>Any exception that arises during destruction should be caught * and logged instead of propagated to the caller of this method. * @param beanName the name of the bean definition * @param beanInstance the bean instance to destroy */
  150. // bean生命周期管理-- 销毁bean
  151. //销毁指定的Bean
  152. void destroyBean(String beanName, Object beanInstance);
  153. /** * Destroy the specified scoped bean in the current target scope, if any. * <p>Any exception that arises during destruction should be caught * and logged instead of propagated to the caller of this method. * @param beanName the name of the scoped bean */
  154. // bean生命周期管理-- 销毁bean
  155. //销毁指定的范围Bean
  156. void destroyScopedBean(String beanName);
  157. /** * Destroy all singleton beans in this factory, including inner beans that have * been registered as disposable. To be called on shutdown of a factory. * <p>Any exception that arises during destruction should be caught * and logged instead of propagated to the caller of this method. */
  158. // bean生命周期管理-- 销毁bean
  159. //销毁所有的单例类
  160. void destroySingletons();
  161. }

发表评论

表情:
评论列表 (有 0 条评论,159人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ConfigurableBeanFactory

    `ConfigurableBeanFactory` 接口包含了一些用于配置 bean 工厂的方法,使开发人员能够更好地控制 bean 的创建和初始化等行为。它继承自 `Hier