此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10! |
这BeanFactory
应用程序接口
这BeanFactory
API 为 Spring 的 IoC 功能提供了底层基础。
其具体合约主要用于与 Spring 的其他部分集成,以及
相关的第三方框架及其DefaultListableBeanFactory
实现
是上级的关键代表GenericApplicationContext
容器。
BeanFactory
和相关接口(例如BeanFactoryAware
,InitializingBean
,DisposableBean
)是其他框架组件的重要集成点。
由于不需要任何注释甚至反射,它们可以非常高效地实现
容器与其组件之间的交互。应用程序级 Bean 可能
使用相同的回调接口,但通常更喜欢声明性依赖
注入,通过注释或编程配置。
请注意,核心BeanFactory
API 级别及其DefaultListableBeanFactory
实现不要对配置格式或任何
要使用的组件注释。所有这些风格都是通过扩展而来的
(例如XmlBeanDefinitionReader
和AutowiredAnnotationBeanPostProcessor
) 和
对共享进行作BeanDefinition
对象作为核心元数据表示形式。
这就是使 Spring 容器如此灵活和可扩展的本质。
BeanFactory
或ApplicationContext
?
本节解释BeanFactory
和ApplicationContext
容器级别及其对引导的影响。
您应该使用ApplicationContext
除非你有充分的理由不这样做,否则使用GenericApplicationContext
及其子类AnnotationConfigApplicationContext
作为自定义引导的常见实现。这些是主要条目
指向 Spring 的核心容器用于所有常见目的:加载配置
文件、触发类路径扫描、以编程方式注册 Bean 定义
和带注释的类,以及(从 5.0 开始)注册功能 Bean 定义。
因为ApplicationContext
包括BeanFactory
是的
通常推荐在普通BeanFactory
,但已满的场景除外
需要控制 bean 处理。在ApplicationContext
(例如GenericApplicationContext
实现),检测到几种 bean
按约定(即按 Bean 名称或按 Bean 类型 - 特别是后处理器),
虽然平原DefaultListableBeanFactory
对任何特殊的豆子都不可知论。
对于许多扩展容器功能,例如注解处理和 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-backed 应用程序中,尤其是当
依靠BeanFactoryPostProcessor
和BeanPostProcessor
扩展实例
典型企业设置中的容器功能。
一 |