InitializingBean,FactoryBean接口在spring源码中的使用代码片段

1.InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,
      凡是继承该接口的类,在初始化bean的时候会执行该方法.
      该方法将在所有的属性被初始化后调用,但是会在init前调用。
	  	  
2.在spring-orm.jar中的SqlMapClientFactoryBean类就使用到InitializingBean接口。
  org.springframework.orm.ibatis.SqlMapClientFactoryBean 类实现了FactoryBean, InitializingBean。
  重写InitializingBean 接口的afterPropertiesSet 方法 得到  com.ibatis.sqlmap.client.SqlMapClient sqlMapClient 实例对象
  
  重写FactoryBean的getObject()方法,返回由InitializingBean接口的afterPropertiesSet方法得到com.ibatis.sqlmap.client.SqlMapClient实例对象.
  
  
以上代码说明:在实际操作spring与ibatis整合过程中,经常遇到在spring配置文件中声明的是
org.springframework.orm.ibatis.SqlMapClientFactoryBean实例,但是在使用的时候却能转为SqlMapClient使用
这是因为Spring的机制的缘故。简单的说:
    如果一个bean实现了 FactoryBean接口,那么Spring就不会把该bean本身实例化并返回,而是返回该bean的getObject()返回的对象。

猜你喜欢

转载自blog.csdn.net/qq_17089617/article/details/80852617