zoukankan      html  css  js  c++  java
  • Bean后置处理器 BeanPostProcessor

    1.BeanPostProcessor接口的作用

    Bean后置处理器允许在调用初始化方法前后对Bean进行额外的处理,Bean后置处理器对IOC容器的所有bean实例逐一处理,而非单一实例。

    我们可以定义一个或多个BeanPostProcessor接口实现类,然后注册到Spring IOC容器中。

    2.BeanPostProcess接口的api

    public interface BeanPostProcessor {
    
        /**
         * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
         * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
         * or a custom init-method). The bean will already be populated with property values.
         */
    //实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务
    @Nullable default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } /** * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean * initialization callbacks (like InitializingBean's {@code afterPropertiesSet} * or a custom init-method). The bean will already be populated with property values. */ //实例化、依赖注入、初始化完毕后执行
    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }

     postProcessorBeforeIntialization方法是在bean实例化,依赖注入之后,但是在自定义初始化-之前调用

       自定义初始化有三种情况----

          ------ 配置文件bean标签添加init-method属性指定初始化方法

          ------@PostConstruct注解指定初始化方法

          -------实现IntializingBean接口的afterPropertiesSet方法

    postProcessorAfterInitialization方法实在bean实例化,依赖注入之后,也在自定义初始化方法之后调用

  • 相关阅读:
    IntelliJ IDEA使用心得之问题篇;
    IntelliJ IDEA使用心得之Maven项目篇
    IntelliJ IDEA使用心得之非Maven项目篇
    IntelliJ IDEA使用心得之插件篇
    IntelliJ IDEA使用心得之快捷键篇
    新博客地址
    【转载】Dijkstra算法和Floyd算法的正确性证明
    【转载】最小生成树之Kruskal算法
    论自动AC机
    【转载】C++ STL priority_queue用法
  • 原文地址:https://www.cnblogs.com/whx7762/p/7850519.html
Copyright © 2011-2022 走看看