此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Framework 6.2.10spring-doc.cadn.net.cn

BeanFactory应用程序接口

BeanFactoryAPI 为 Spring 的 IoC 功能提供了底层基础。 其具体合约主要用于与 Spring 的其他部分集成,以及 相关的第三方框架及其DefaultListableBeanFactory实现 是上级的关键代表GenericApplicationContext容器。spring-doc.cadn.net.cn

BeanFactory和相关接口(例如BeanFactoryAware,InitializingBean,DisposableBean)是其他框架组件的重要集成点。 由于不需要任何注释甚至反射,它们可以非常高效地实现 容器与其组件之间的交互。应用程序级 Bean 可能 使用相同的回调接口,但通常更喜欢声明性依赖 注入,通过注释或编程配置。spring-doc.cadn.net.cn

请注意,核心BeanFactoryAPI 级别及其DefaultListableBeanFactory实现不要对配置格式或任何 要使用的组件注释。所有这些风格都是通过扩展而来的 (例如XmlBeanDefinitionReaderAutowiredAnnotationBeanPostProcessor) 和 对共享进行作BeanDefinition对象作为核心元数据表示形式。 这就是使 Spring 容器如此灵活和可扩展的本质。spring-doc.cadn.net.cn

BeanFactoryApplicationContext?

本节解释BeanFactoryApplicationContext容器级别及其对引导的影响。spring-doc.cadn.net.cn

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

因为ApplicationContext包括BeanFactory是的 通常推荐在普通BeanFactory,但已满的场景除外 需要控制 bean 处理。在ApplicationContext(例如GenericApplicationContext实现),检测到几种 bean 按约定(即按 Bean 名称或按 Bean 类型 - 特别是后处理器), 虽然平原DefaultListableBeanFactory对任何特殊的豆子都不可知论。spring-doc.cadn.net.cn

对于许多扩展容器功能,例如注解处理和 AOP 代理, 这BeanPostProcessor扩展点是必不可少的。 如果您只使用普通DefaultListableBeanFactory,此类后处理器不会 默认情况下被检测并激活。这种情况可能会令人困惑,因为 您的 bean 配置实际上没有任何问题。相反,在这种情况下, 容器需要通过其他设置完全引导。spring-doc.cadn.net.cn

下表列出了BeanFactoryApplicationContext接口和实现。spring-doc.cadn.net.cn

表 1.功能矩阵
特征 BeanFactory ApplicationContext

Bean 实例化/布线spring-doc.cadn.net.cn

是的spring-doc.cadn.net.cn

是的spring-doc.cadn.net.cn

集成生命周期管理spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

是的spring-doc.cadn.net.cn

自动BeanPostProcessor注册spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

是的spring-doc.cadn.net.cn

自动BeanFactoryPostProcessor注册spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

是的spring-doc.cadn.net.cn

方便MessageSource访问(用于国际化)spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

是的spring-doc.cadn.net.cn

内置ApplicationEvent发布机制spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

是的spring-doc.cadn.net.cn

DefaultListableBeanFactory, 您需要以编程方式调用addBeanPostProcessor,如以下示例所示:spring-doc.cadn.net.cn

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方法,如以下示例所示:spring-doc.cadn.net.cn

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 应用程序中,尤其是当 依靠BeanFactoryPostProcessorBeanPostProcessor扩展实例 典型企业设置中的容器功能。spring-doc.cadn.net.cn

AnnotationConfigApplicationContext具有所有常见的注释后处理器 注册,并可能在 通过配置注释覆盖,例如@EnableTransactionManagement. 在 Spring 基于注释的配置模型的抽象级别, Bean 后处理器的概念仅仅成为一个内部容器细节。spring-doc.cadn.net.cn