InitializingBean接口

public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}

InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法。

凡是继承该接口的类,在初始化bean的时候 afterPropertiesSet 方法将在所有的属性被初始化后调用。

 

实现InitializingBean接口与在配置文件中指定init-method有什么不同?

<bean id="testInitializingBean" class="com.TestInitializingBean" init-method="testInit"></bean>

在spring初始化bean的时候,如果该bean是实现了InitializingBean接口,并且同时在配置文件中指定了init-method,

系统则是先调用afterPropertiesSet方法,然后在调用init-method中指定的方法。

 

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

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

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

 

posted on 2018-02-21 12:22  HB1  阅读(110)  评论(0)    收藏  举报

导航