Spring Core 之 Container Extension Points(容器扩展点)


生词汇总:

  • and so forth ---------------------------------------- 等等
  • such ------------------------------------------ 这样, 这么, 其
  • semantics ---------------------------------------- 语义
  • verbose ---------------------------------------- 冗长的

一、Customizing Beans by Using a BeanPostProcessor(使用BeanPostProcessor自定义bean)

The BeanPostProcessor interface defines callback methods that you can implement to provide your own (or override the container’s default) instantiation logic, dependency resolution logic, and so forth. If you want to implement some custom logic after the Spring container finishes instantiating, configuring, and initializing a bean, you can plug in one or more custom BeanPostProcessor implementations.You can configure multiple BeanPostProcessor instances, and you can control the order in which these BeanPostProcessor instances run by setting the order property. You can set this property only if the BeanPostProcessor implements the Ordered interface. If you write your own BeanPostProcessor, you should consider implementing the Ordered interface, too. BeanPostProcessor instances are scoped per-container.it post-processes only the beans in that container.

BeanPostProcessor接口定义了回调方法,使你能够使用自己的实例化逻辑、依赖解析逻辑等等。如果你想在容器实例化、配置和初始化一个bean之后实现一些自定义逻辑,你可以使用一个或多个自定义的BeanPostProcessor实现。你可以配置多个BeanPostProcessor实例,同时你可以通过设置order属性控制这些BeanPostProcessor实例的运行顺序,仅当BeanPostProcessor实现了Orderd接口时可以设置order属性,因此如果你编写你自己的BeanPostProcessor时,也应该考虑实现Orderd接口。BeanPostProcessor 实例的作用域是每个容器。它只对容器中的 bean 进行后处理。

The org.springframework.beans.factory.config.BeanPostProcessor interface consists of exactly two callback methods. When such a class is registered as a post-processor with the container, for each bean instance that is created by the container, the post-processor gets a callback from the container both before container initialization methods (such as InitializingBean.afterPropertiesSet() or any declared init method) are called, and after any bean initialization callbacks. The post-processor can take any action with the bean instance, including ignoring the callback completely. A bean post-processor typically checks for callback interfaces, or it may wrap a bean with a proxy. Some Spring AOP infrastructure classes are implemented as bean post-processors in order to provide proxy-wrapping logic.

org.springframework.beans.factory.config.BeanPostProcessor 接口由两个回调方法组成。当这样的一个类在容器中被注册为post-processor时,对由容器创建的每一个bean来说,post-processor在容器任何bean的初始化方法被调用之前和初始化之后时会获得一个回调。post-processor可以对bean实例进行任意操作,包括完全忽略掉其回调。post-processor bean通常检查回调接口,或者它可以用代理包装 bean。一些 Spring AOP 基础设施类被实现为post-processor bean,以提供代理包装逻辑。

package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;

public interface BeanPostProcessor {
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}

二、Customizing Configuration Metadata with a BeanFactoryPostProcessor(通过BeanFactoryPostProcessor自定义配置信息)

The semantics of the org.springframework.beans.factory.config.BeanFactoryPostProcessor interface are similar to those of the BeanPostProcessor, with one major difference: BeanFactoryPostProcessor operates on the bean configuration metadata. That is, the Spring IoC container lets a BeanFactoryPostProcessor read the configuration metadata and potentially change it before the container instantiates any beans other than BeanFactoryPostProcessor instances.You can configure multiple BeanFactoryPostProcessor instances, and you can control the order in which these BeanFactoryPostProcessor instances run by setting the order property. However, you can only set this property if the BeanFactoryPostProcessor implements the Ordered interface. If you write your own BeanFactoryPostProcessor, you should consider implementing the Ordered interface, too.Also, BeanFactoryPostProcessor instances are scoped per-container.

org.springframework.beans.factory.config.BeanFactoryPostProcessor接口的语义和BeanPostProcessor相似,但有一个主要的区别:BeanFactoryPostProcessor操作的是bean的配置元数据。也就是说,Spring IOC容器使BeanFactoryPostProcessor在容器初始化任何非BeanFactoryPostProcessor bean之前读取bean的配置元数据信息并能修改它们。你可以配置多个BeanFactoryPostProcessor 实现,同时你也可以通过设置order属性控制这些BeanFactoryPostProcessor 实例的运行顺序,仅当BeanFactoryPostProcessor 实现了Orderd接口时可以设置order属性,因此如果你编写你自己的BeanFactoryPostProcessor 时,也应该考虑实现Orderd接口。此外, BeanFactoryPostProcessor 实例的作用范围也是一个容器。

A bean factory post-processor is automatically run when it is declared inside an ApplicationContext, in order to apply changes to the configuration metadata that define the container. Spring includes a number of predefined bean factory post-processors, such as PropertyOverrideConfigurer and PropertySourcesPlaceholderConfigurer. You can also use a custom BeanFactoryPostProcessor — for example, to register custom property editors.

当一个factory post-processor bean被声明在ApplicationContext时,它会自动运行,为容器中定义的配置元数据做一些改变。Spring 包含许多预定义的 factory post-processor bean ,例如 PropertyOverrideConfigurer 和 PropertySourcesPlaceholderConfigurer。你还可以使用自定义 BeanFactoryPostProcessor ,例如,注册自定义属性编辑器。

package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;

@FunctionalInterface
public interface BeanFactoryPostProcessor {
    void postProcessBeanFactory(ConfigurableListableBeanFactory var1) throws BeansException;
}

三、Customizing Instantiation Logic with a FactoryBean(通过FactoryBean自定义实例化逻辑)

You can implement the org.springframework.beans.factory.FactoryBean interface for objects that are themselves factories.The FactoryBean interface is a point of pluggability into the Spring IoC container’s instantiation logic. If you have complex initialization code that is better expressed in Java as opposed to a (potentially) verbose amount of XML, you can create your own FactoryBean, write the complex initialization inside that class, and then plug your custom FactoryBean into the container.
The FactoryBean interface provides three methods:

你可以为本身是工厂的对象实现 org.springframework.beans.factory.FactoryBean 接口。FactoryBean 接口是 Spring IoC 容器实例化逻辑的可插入点。如果你有复杂的初始化代码,可以用 Java 更好地表达,而不是(可能)冗长的 XML,你就可以创建自己的 FactoryBean,在该类中编写复杂的初始化,然后将自定义 FactoryBean 插入容器中。 FactoryBean 接口提供了三种方法:

package org.springframework.beans.factory;

import org.springframework.lang.Nullable;

public interface FactoryBean<T> {
    String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";

    @Nullable
    T getObject() throws Exception;

    @Nullable
    Class<?> getObjectType();

    default boolean isSingleton() {
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值