The `BeanFactory` API

1.16. The BeanFactory API

The BeanFactory API provides the underlying basis for Spring’s IoC functionality. Its specific contracts are mostly used in integration with other parts of Spring and related third-party frameworks, and its DefaultListableBeanFactory implementation is a key delegate within the higher-level GenericApplicationContext container.

BeanFactory API为Spring的IoC功能提供了底层基础。它的特定契约主要用于与Spring的其他部分和相关的第三方框架集成,它的DefaultListableBeanFactory实现是高级GenericApplicationContext容器中的一个关键委托。

BeanFactory and related interfaces (such as BeanFactoryAware, InitializingBean, DisposableBean) are important integration points for other framework components. By not requiring any annotations or even reflection, they allow for very efficient interaction between the container and its components. Application-level beans may use the same callback interfaces but typically prefer declarative dependency injection instead, either through annotations or through programmatic configuration.

BeanFactory和相关接口(如BeanFactoryAware、InitializingBean、DisposableBean)是其他框架组件的重要集成点。由于不需要任何注释甚至反射,它们允许在容器及其组件之间进行非常高效的交互。应用程序级bean可以使用相同的回调接口,但通常更喜欢声明性依赖注入,或者通过注释,或者通过编程配置。

Note that the core BeanFactory API level and its DefaultListableBeanFactory implementation do not make assumptions about the configuration format or any component annotations to be used. All of these flavors come in through extensions (such as XmlBeanDefinitionReader and AutowiredAnnotationBeanPostProcessor) and operate on shared BeanDefinition objects as a core metadata representation. This is the essence of what makes Spring’s container so flexible and extensible.

请注意,核心BeanFactory API级别及其DefaultListableBeanFactory实现不假设要使用的配置格式或任何组件注释。所有这些风格都是通过扩展(如XmlBeanDefinitionReader和AutowiredAnnotationBeanPostProcessor)来实现的,并将共享BeanDefinition对象作为核心元数据表示来操作。这是使Spring的容器如此灵活和可扩展的本质。

1.16.1. `BeanFactory` or `ApplicationContext`?

This section explains the differences between the BeanFactory and ApplicationContext container levels and the implications on bootstrapping.

本节解释BeanFactory和ApplicationContext容器级别之间的区别以及引导的含义。

You should use an ApplicationContext unless you have a good reason for not doing so, with GenericApplicationContext and its subclass AnnotationConfigApplicationContext as the common implementations for custom bootstrapping. These are the primary entry points to Spring’s core container for all common purposes: loading of configuration files, triggering a classpath scan, programmatically registering bean definitions and annotated classes, and (as of 5.0) registering functional bean definitions.

您应该使用ApplicationContext,除非您有很好的理由不这样做,使用GenericApplicationContext及其子类AnnotationConfigApplicationContext作为自定义引导的通用实现。这些是Spring核心容器的主要入口点,用于所有常见目的:加载配置文件、触发类路径扫描、以编程方式注册bean定义和注释类,以及(从5.0开始)注册功能性bean定义。

Because an ApplicationContext includes all the functionality of a BeanFactory, it is generally recommended over a plain BeanFactory, except for scenarios where full control over bean processing is needed. Within an ApplicationContext (such as the GenericApplicationContext implementation), several kinds of beans are detected by convention (that is, by bean name or by bean type — in particular, post-processors), while a plain DefaultListableBeanFactory is agnostic about any special beans.

因为ApplicationContext包含BeanFactory的所有功能,所以除了需要对bean处理进行完全控制的场景外,一般建议使用ApplicationContext而不是普通BeanFactory。在ApplicationContext(例如GenericApplicationContext实现)中,有几种类型的bean是通过约定(即通过bean名称或bean类型—特别是后处理器)检测的,而普通的DefaultListableBeanFactory则与任何特殊的bean无关。

For many extended container features, such as annotation processing and AOP proxying, the BeanPostProcessor extension point is essential. If you use only a plain DefaultListableBeanFactory, such post-processors do not get detected and activated by default. This situation could be confusing, because nothing is actually wrong with your bean configuration. Rather, in such a scenario, the container needs to be fully bootstrapped through additional setup.

对于许多扩展容器特性,例如注释处理和AOP代理,BeanPostProcessor扩展点是必不可少的。如果只使用普通的DefaultListableBeanFactory,则默认情况下不会检测和激活这种后处理器。这种情况可能令人困惑,因为您的bean配置实际上没有任何错误。相反,在这种情况下,需要通过额外的设置来完全引导容器。

The following table lists features provided by the BeanFactory and ApplicationContext interfaces and implementations.

下表列出了BeanFactory和ApplicationContext接口和实现提供的特性。
Feature BeanFactory ApplicationContext
Bean instantiation/wiring Yes Yes
Integrated lifecycle management No Yes
Automatic BeanPostProcessor registration No Yes
Automatic BeanFactoryPostProcessor registration No Yes
Convenient MessageSource access (for internationalization) No Yes
Built-in ApplicationEvent publication mechanism No Yes

To explicitly register a bean post-processor with a DefaultListableBeanFactory, you need to programmatically call addBeanPostProcessor, as the following example shows:

要显式地用DefaultListableBeanFactory注册一个bean后处理器,您需要以编程方式调用addBeanPostProcessor,如下例所示:
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

To apply a BeanFactoryPostProcessor to a plain DefaultListableBeanFactory, you need to call its postProcessBeanFactory method, as the following example shows:

要将BeanFactoryPostProcessor应用到普通的DefaultListableBeanFactory,您需要调用它的postProcessBeanFactory方法,如下面的示例所示:
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);

In both cases, the explicit registration steps are inconvenient, which is why the various ApplicationContext variants are preferred over a plain DefaultListableBeanFactory in Spring-backed applications, especially when relying on BeanFactoryPostProcessor and BeanPostProcessor instances for extended container functionality in a typical enterprise setup.

在这两种情况下,显式注册步骤都是不方便的,这就是为什么在spring支持的应用程序中,各种ApplicationContext变量优于普通的DefaultListableBeanFactory,特别是在典型企业设置中依赖BeanFactoryPostProcessor和BeanPostProcessor实例来实现扩展容器功能时。

An AnnotationConfigApplicationContext has all common annotation post-processors registered and may bring in additional processors underneath the covers through configuration annotations, such as @EnableTransactionManagement. At the abstraction level of Spring’s annotation-based configuration model, the notion of bean post-processors becomes a mere internal container detail.

AnnotationConfigApplicationContext注册了所有公共注释后处理器,并可能通过配置注释引入额外的处理器,例如@EnableTransactionManagement。在Spring基于注释的配置模型的抽象级别,bean后处理器的概念仅仅是内部容器的细节。
posted @ 2022-09-13 17:36  丶Jan  阅读(16)  评论(0)    收藏  举报