spring 初始化 Bean的方式及相关应用

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

 

 

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

 

 

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

 

 

4:TransactionTemplate实现InitializingBean接口,主要是判断transactionManager是否已经初始化,如果没有则抛出异常。源码如下:

public void afterPropertiesSet() {

if (this.transactionManager == null) {

throw new IllegalArgumentException("Property 'transactionManager' is required");

}

}

你可能感兴趣的:(spring)