spring系列5:PostProcessor(后置处理器)

前言

前面介绍 Spring 的基石:BeanDefinition。以及 BeanDefinition 的管理者:BeanFactory,还有一个 BeanDefinition 注册接口:BeanDefinitionRegistry

这些相当于工具,那谁来操作这些工具呢?

幕后的操作者就是 Spring 中后置处理器(PostProcessor)

后置处理器分为两种:BeanFactoryPostProcessor(Bean 工厂后置处理器) 和 BeanPostProcessor(Bean 后置处理器)

下面简述一下它们的作用

BeanFactoryPostProcessor接口

Factory hook that allows for custom modification of an application context's bean definitions, adapting the bean property values of the context's underlying bean factory.
public interface BeanFactoryPostProcessor {

   /**
    * Modify the application context's internal bean factory after its standard
    * initialization. All bean definitions will have been loaded, but no beans
    * will have been instantiated yet. This allows for overriding or adding
    * properties even to eager-initializing beans.
    * @param beanFactory the bean factory used by the application context
    * @throws org.springframework.beans.BeansException in case of errors
    */
   void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

BeanFactoryPostProcessor 接口里面只有一个 postProcessBeanFactory 方法,结合接口的注释和接口里的方法注释,可以得出,这个接口是一个 BeanFactory 钩子接口,实现该接口,可以通过 postProcessBeanFactory 方法在 BeanFactory 实例化后,Bean 实例化前调用该接口,对 BeanFactory 中的 BeanDefinition 或 BeanDefinition 的元数据进行修改。

BeanDefinitionRegistryPostProcessor

Extension to the standard BeanFactoryPostProcessor SPI, allowing for the registration of further bean definitions before regular BeanFactoryPostProcessor detection kicks in.
public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor {

   /**
    * Modify the application context's internal bean definition registry after its
    * standard initialization. All regular bean definitions will have been loaded,
    * but no beans will have been instantiated yet. This allows for adding further
    * bean definitions before the next post-processing phase kicks in.
    * @param registry the bean definition registry used by the application context
    * @throws org.springframework.beans.BeansException in case of errors
    */
   void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException;

}

BeanDefinitionRegistryPostProcessor 继承了 BeanFactoryPostProcessor,在 Bean 实例化和检测之前,实现了一个非常重要的功能,动态注册一个 BeanDefinition,除此之外,你还可以修改已注册的 BeanDefinition 信息。

BeanPostProcessor

Factory hook that allows for custom modification of new bean instances —for example, checking for marker interfaces or wrapping beans with proxies.
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;
   }

}

BeanPostProcessor 是 Bean 后置处理器接口,里面包含两个方法:postProcessBeforeInitialization 和 postProcessAfterInitialization。前者在 Bean 实例化前调用,后者则是在实例化后。比如生成一个 Bean 的代理类,就是其子类做的。

总结

别小看上面三个接口,Spring 绝大部分重要技术都是这三个接口的子类来实现,它们也是扩展接口,可以自定义类来实现它们,以增强 Spring 功能或者是扩展 Spring 功能,又或是第三方框架。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMeter后置处理器Post-Processor)是一种用于处理采样器(Sampler)的响应数据的工具。后置处理器通常用于从响应中提取关键数据,并将提取的数据应用于后续的请求或其他操作。 JMeter提供了多种后置处理器,常用的包括: 1. 正则表达式提取器(Regular Expression Extractor):通过正则表达式从响应中提取指定的文本数据。可以使用正则表达式定义要提取的数据的模式,并将提取的结果保存到变量中。 2. XPath提取器(XPath Extractor):通过XPath表达式从XML响应中提取指定的节点或属性值。可以使用XPath表达式指定要提取的节点或属性,并将提取的结果保存到变量中。 3. JSON提取器(JSON Extractor):通过JSON路径表达式从JSON响应中提取指定的数据。可以使用JSON路径表达式指定要提取的数据,并将提取的结果保存到变量中。 4. Boundary Extractor:从响应中提取两个边界之间的文本数据。可以指定开始和结束边界,并将边界之间的内容保存到变量中。 使用后置处理器的步骤如下: 1. 在测试计划中选择需要添加后置处理器的采样器。 2. 右键单击采样器,选择"Add",然后选择"Post-Processor"。 3. 在弹出的对话框中,选择要使用的后置处理器类型。 4. 配置后置处理器的参数,如正则表达式、XPath表达式或JSON路径等。 5. 可选:将提取的数据应用于后续的请求或其他操作,可以使用变量引用方式。 后置处理器可以帮助我们从采样器的响应中提取所需的数据,并在后续的测试步骤中使用。这对于模拟用户登录、提取会话ID、动态生成参数等场景非常有用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值