此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
这BeanFactory
应用程序接口
这BeanFactory
API 为 Spring 的 IoC 功能提供了基础。
它的特定 Contract 主要用于与 Spring 和
相关的第三方框架及其DefaultListableBeanFactory
实现
是更高级别中的关键委托GenericApplicationContext
容器。
BeanFactory
和相关接口(如BeanFactoryAware
,InitializingBean
,DisposableBean
) 是其他框架组件的重要集成点。
通过不需要任何注释甚至反射,它们允许非常高效
容器与其组件之间的交互。应用程序级 bean 可以
使用相同的回调接口,但通常更喜欢声明性依赖项
注入,而是通过 Comments 或编程配置。
请注意,核心BeanFactory
API 级别及其DefaultListableBeanFactory
implementation 不要对配置格式或任何
要使用的组件注释。所有这些风格都来自扩展
(例如XmlBeanDefinitionReader
和AutowiredAnnotationBeanPostProcessor
) 和
对共享作BeanDefinition
对象作为核心元数据表示形式。
这就是 Spring 的容器如此灵活和可扩展的本质。
BeanFactory
或ApplicationContext
?
本节介绍了BeanFactory
和ApplicationContext
容器级别及其对引导程序的影响。
您应该使用ApplicationContext
除非您有充分的理由不这样做,否则使用GenericApplicationContext
及其子类AnnotationConfigApplicationContext
作为自定义引导的常见实现。这些是主要条目
指向 Spring 的核心容器,用于所有常见目的:加载配置
文件, 触发类路径扫描, 以编程方式注册 Bean 定义
和带 Comments 的类,以及(从 5.0 开始)注册函数式 bean 定义。
因为ApplicationContext
包括BeanFactory
是的
通常推荐于平原BeanFactory
,但 full
需要控制 Bean 处理。在ApplicationContext
(例如GenericApplicationContext
implementation 中),会检测到几种 bean
按约定(即按 Bean 名称或 Bean 类型 — 特别是后处理器),
虽然普通的DefaultListableBeanFactory
对任何特殊 bean 都不可知。
对于许多扩展容器功能,例如注释处理和 AOP 代理,
这BeanPostProcessor
扩展点是必不可少的。
如果您只使用普通DefaultListableBeanFactory
,则此类后处理器不会
默认情况下被检测并激活。这种情况可能会令人困惑,因为
您的 bean 配置实际上没有任何问题。相反,在这种情况下,
容器需要通过其他设置完全引导。
下表列出了BeanFactory
和ApplicationContext
接口和实现。
特征 | BeanFactory |
ApplicationContext |
---|---|---|
Bean 实例化/连接 |
是的 |
是的 |
集成的生命周期管理 |
不 |
是的 |
自动 |
不 |
是的 |
自动 |
不 |
是的 |
方便 |
不 |
是的 |
内置 |
不 |
是的 |
要使用DefaultListableBeanFactory
,
您需要以编程方式调用addBeanPostProcessor
,如下例所示:
-
Java
-
Kotlin
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
// populate the factory with bean definitions
// now register any needed BeanPostProcessor instances
factory.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor());
factory.addBeanPostProcessor(new MyBeanPostProcessor());
// now start using the factory
val factory = DefaultListableBeanFactory()
// populate the factory with bean definitions
// now register any needed BeanPostProcessor instances
factory.addBeanPostProcessor(AutowiredAnnotationBeanPostProcessor())
factory.addBeanPostProcessor(MyBeanPostProcessor())
// now start using the factory
要应用BeanFactoryPostProcessor
到平原DefaultListableBeanFactory
,
你需要调用它的postProcessBeanFactory
方法,如下例所示:
-
Java
-
Kotlin
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new FileSystemResource("beans.xml"));
// bring in some property values from a Properties file
PropertySourcesPlaceholderConfigurer cfg = new PropertySourcesPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
// now actually do the replacement
cfg.postProcessBeanFactory(factory);
val factory = DefaultListableBeanFactory()
val reader = XmlBeanDefinitionReader(factory)
reader.loadBeanDefinitions(FileSystemResource("beans.xml"))
// bring in some property values from a Properties file
val cfg = PropertySourcesPlaceholderConfigurer()
cfg.setLocation(FileSystemResource("jdbc.properties"))
// now actually do the replacement
cfg.postProcessBeanFactory(factory)
在这两种情况下,显式注册步骤都很不方便,即
为什么各种ApplicationContext
变体优于普通DefaultListableBeanFactory
在 Spring 支持的应用程序中,尤其是当
依靠BeanFactoryPostProcessor
和BeanPostProcessor
extended 的实例
典型企业设置中的容器功能。
一 |