zoukankan      html  css  js  c++  java
  • spring 的 InitializingBean

    public interface InitializingBean {

    /**
    * Invoked by a BeanFactory after it has set all bean properties supplied
    * (and satisfied BeanFactoryAware and ApplicationContextAware).
    * <p>This method allows the bean instance to perform initialization only
    * possible when all bean properties have been set and to throw an
    * exception in the event of misconfiguration.
    * @throws Exception in the event of misconfiguration (such
    * as failure to set an essential property) or if initialization fails.
    */
    void afterPropertiesSet() throws Exception;

    }

    如果想在bean初始化的时候做一些操作,则可以继承自InitializingBean 接口;

    这个接口只有一个方法就是afterPropertiesSet 作用就是当bean的各个属性初始化之后,会调用此方法

    spring的具体实现是在AbstractAutowireCapableBeanFactory类中 invokeInitMethods方法会调用 afterPropertiesSet 

    BeanNameAware 获得到容器中Bean的名称
    BeanFactoryAware 获得当前bean Factory,从而调用容器的服务
    ApplicationContextAware 当前的application context从而调用容器的服务
    MessageSourceAware 得到message source从而得到文本信息
    ApplicationEventPublisherAware 应用时间发布器,用于发布事件
    ResourceLoaderAware 获取资源加载器,可以获得外部资源文件

  • 相关阅读:
    ABP源码分析四十:ZERO的Application和Tenant
    ABP源码分析三十九:ABP.Hangfire
    ABP源码分析三十八: ABP.Web.Api.OData
    ABP源码分析三十七:ABP.Web.Api Script Proxy API
    232. 用栈实现队列
    155. 最小栈
    725. 分隔链表
    328. 奇偶链表
    148. 排序链表
    143. 重排链表
  • 原文地址:https://www.cnblogs.com/liguangming/p/9645396.html
Copyright © 2011-2022 走看看