zoukankan      html  css  js  c++  java
  • InitializingBean

    org.springframework.beans.factory包下有一个接口是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;

    这个方法将在所有的属性被初始化后调用。

    但是会在init前调用。

    但是主要的是如果是延迟加载的话,则马上执行。

    所以可以在类上加上注解:

    import org.springframework.context.annotation.Lazy;

    @Lazy(false)

    这样spring容器初始化的时候afterPropertiesSet就会被调用。

    1:spring为bean提供了两种初始化bean的方式,实现InitializingBean接口,实现afterPropertiesSet方法,或者在配置文件中同过init-method指定,两种方式可以同时使用

    2:实现InitializingBean接口是直接调用afterPropertiesSet方法,比通过反射调用init-method指定的方法效率相对来说要高点。但是init-method方式消除了对spring的依赖

    由结果可看出,在spring初始化bean的时候,如果该bean是实现了InitializingBean接口,并且同时在配置文件中指定了init-method,系统则是先调用afterPropertiesSet方法,然后在调用init-method中指定的方法。

    3:如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。

  • 相关阅读:
    p67 把特定位置的位变成0,进行与运算
    p65 逻辑与运算
    JavaScript对象和初始面向对象
    JavaScript操作DOM对象
    JavaScript操作BOM对象
    JavaScript基础
    DAO模式
    使用ADO.NET访问数据库
    连接查询和分组查询
    模糊查询和聚合函数
  • 原文地址:https://www.cnblogs.com/YDDMAX/p/5198721.html
Copyright © 2011-2022 走看看