API 为 Spring 的 IoC 功能提供了基础。 它的特定合约主要用于与 Spring 和 相关的第三方框架及其实现 是更高级别容器中的键委托。BeanFactoryDefaultListableBeanFactoryGenericApplicationContextSpring中文文档

BeanFactory和相关接口(如 、 、 )是其他框架组件的重要集成点。 由于不需要任何注释甚至反射,它们可以非常有效地实现 容器与其组件之间的交互。应用程序级 bean 可能 使用相同的回调接口,但通常更喜欢声明性依赖项 相反,通过注释或编程配置进行注入。BeanFactoryAwareInitializingBeanDisposableBeanSpring中文文档

请注意,核心 API 级别及其实现不会对配置格式或任何 要使用的组件注释。所有这些口味都是通过扩展进入的 (例如 和 )和 将共享对象作为核心元数据表示形式进行操作。 这就是 Spring 容器如此灵活和可扩展的本质。BeanFactoryDefaultListableBeanFactoryXmlBeanDefinitionReaderAutowiredAnnotationBeanPostProcessorBeanDefinitionSpring中文文档

BeanFactory或?ApplicationContext

本部分介绍容器级别和容器级别之间的差异以及对引导的影响。BeanFactoryApplicationContextSpring中文文档

除非有充分的理由不这样做,否则应该使用 an 及其子类作为自定义引导的常见实现。这些是主要条目 指向 Spring 的核心容器,用于所有常见目的:加载配置 文件, 触发类路径扫描, 以编程方式注册 Bean 定义 和带注释的类,以及(从 5.0 开始)注册功能 Bean 定义。ApplicationContextGenericApplicationContextAnnotationConfigApplicationContextSpring中文文档

因为 an 包含 a 的所有功能,所以它是 一般推荐在普通 上,除非是满的 需要控制 Bean 加工。在一个(例如实现)中,检测到几种 bean 根据约定(即按 Bean 名称或按 Bean 类型,特别是后处理器), 而平原对任何特殊的豆子都是不可知的。ApplicationContextBeanFactoryBeanFactoryApplicationContextGenericApplicationContextDefaultListableBeanFactorySpring中文文档

对于许多扩展容器功能,例如注释处理和 AOP 代理, BeanPostProcessor 扩展点是必不可少的。 如果你只使用一个普通的,这样的后处理器不会 默认情况下被检测并激活。这种情况可能会令人困惑,因为 您的 Bean 配置实际上没有任何问题。相反,在这种情况下, 容器需要通过其他设置完全引导。DefaultListableBeanFactorySpring中文文档

下表列出了 和 接口和实现提供的功能。BeanFactoryApplicationContextSpring中文文档

表 1.功能矩阵
特征 BeanFactory ApplicationContext

Bean 实例化/布线Spring中文文档

是的Spring中文文档

是的Spring中文文档

集成生命周期管理Spring中文文档

Spring中文文档

是的Spring中文文档

自动注册BeanPostProcessorSpring中文文档

Spring中文文档

是的Spring中文文档

自动注册BeanFactoryPostProcessorSpring中文文档

Spring中文文档

是的Spring中文文档

方便的访问(用于国际化)MessageSourceSpring中文文档

Spring中文文档

是的Spring中文文档

内置发布机制ApplicationEventSpring中文文档

Spring中文文档

是的Spring中文文档

要显式注册一个 Bean 后处理器,请使用 , 您需要以编程方式调用 ,如以下示例所示:DefaultListableBeanFactoryaddBeanPostProcessorSpring中文文档

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

要将 a 应用于普通 , 您需要调用其方法,如以下示例所示:BeanFactoryPostProcessorDefaultListableBeanFactorypostProcessBeanFactorySpring中文文档

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)

在这两种情况下,显式注册步骤都很不方便,这是 为什么在 Spring 支持的应用程序中,各种变体比普通变体更受欢迎,尤其是当 依赖 和 实例进行扩展 典型企业设置中的容器功能。ApplicationContextDefaultListableBeanFactoryBeanFactoryPostProcessorBeanPostProcessorSpring中文文档

具有所有通用的注释后处理器 注册,并可能在 涵盖配置注释,例如 . 在 Spring 基于注解的配置模型的抽象级别, Bean 后处理器的概念变成了一个内部容器的细节。AnnotationConfigApplicationContext@EnableTransactionManagementSpring中文文档

表 1.功能矩阵
特征 BeanFactory ApplicationContext

Bean 实例化/布线Spring中文文档

是的Spring中文文档

是的Spring中文文档

集成生命周期管理Spring中文文档

Spring中文文档

是的Spring中文文档

自动注册BeanPostProcessorSpring中文文档

Spring中文文档

是的Spring中文文档

自动注册BeanFactoryPostProcessorSpring中文文档

Spring中文文档

是的Spring中文文档

方便的访问(用于国际化)MessageSourceSpring中文文档

Spring中文文档

是的Spring中文文档

内置发布机制ApplicationEventSpring中文文档

Spring中文文档

是的Spring中文文档

具有所有通用的注释后处理器 注册,并可能在 涵盖配置注释,例如 . 在 Spring 基于注解的配置模型的抽象级别, Bean 后处理器的概念变成了一个内部容器的细节。AnnotationConfigApplicationContext@EnableTransactionManagementSpring中文文档