美文网首页
Spring Boot 源码分析 —— 后置处理器

Spring Boot 源码分析 —— 后置处理器

作者: 怪咖_OOP | 来源:发表于2018-09-13 21:09 被阅读0次

    [TOC]

    在spring中关于后置处理器主要有两个接口BeanFactoryPostProcessor、BeanPostProcessor。

    BeanFactoryPostProcessor与BeanPostProcessor的区别

    引用spring源码中的类注释

    BeanFactoryPostProcessor类注释

    /**
     * Allows for custom modification of an application context's bean definitions,
     * adapting the bean property values of the context's underlying bean factory.
     *
     * <p>Application contexts can auto-detect BeanFactoryPostProcessor beans in
     * their bean definitions and apply them before any other beans get created.
     ......
     *
     * <p>A BeanFactoryPostProcessor may interact with and modify bean
     * definitions, but never bean instances. Doing so may cause premature bean
     * instantiation, violating the container and causing unintended side-effects.
     */
    

    允许自定义修改应用程序上下文的bean定义,适配上下文中bean工厂属性的潜在bean。
    应用程序上下文可以在它们的bean定义中自动检测BeanFactoryPostProcessor 实例,并在创建任何其他bean之前应用它们。
    一个BeanFactoryPostProcessor后置处理器可以与bean定义交互和修改,但不能与bean实例交互修改。这样做可能导致过早bean实例化,造成意想不到的副作用。如果需要bean实例交互,请考虑实现BeanPostProcessor。

    BeanPostProcessor类注释

    /**
     * Factory hook that allows for custom modification of new bean instances,
     * e.g. checking for marker interfaces or wrapping them with proxies.
     *
     * <p>ApplicationContexts can autodetect BeanPostProcessor beans in their
     * bean definitions and apply them to any beans subsequently created.
     * Plain bean factories allow for programmatic registration of post-processors,
     * applying to all beans created through this factory.
        ......
     */
    

    工厂钩子,允许自定义修改新的bean实例;ApplicationContexts可以自动检测BeanPostProcessor bean定义中的BeanPostProcessor 实例,并将它们应用于随后创建的任何bean;普通bean工厂允许对后处理器进行编程注册,应用于通过该工厂创建的所有bean

    总结

    BeanFactoryPostProcessor:作用在容器内所有bean实例化之前;可以修改应用程序上下文的bean定义

    BeanPostProcessor:作用在容器内所有bean实例化之后初始化之前;允许修改bean实例

    ▲注意:
    BeanFactoryPostProcessor修改bean定义,BeanPostProcessor修改bean实例

    以上属于原创文章,转载请注明作者@怪咖
    QQ:208275451

    相关文章

      网友评论

          本文标题:Spring Boot 源码分析 —— 后置处理器

          本文链接:https://www.haomeiwen.com/subject/sgawgftx.html