SqlMapClientFactoryBean为什么能转为SqlMapClient

来源:互联网 发布:淘宝天天特价女装 编辑:程序博客网 时间:2024/04/30 20:14

在实际操作spring与mybatis整合过程中,经常遇到在spring配置文件中声明的是org.springframework.orm.ibatis.SqlMapClientFactoryBean实例,但是在使用的时候却能转为SqlMapClient使用。原因如下:


这是因为Spring的机制的缘故。简单的说,如果一个bean实现了 FactoryBean接口,那么Spring就不会把该bean本身实例化并返回,而是返回该bean的getObject()返回的对象。这是Sprign的游戏规则。我们来看一眼 SqlMapClientFactoryBean的源码片段:


 public class SqlMapClientFactoryBean implements  FactoryBean,InitializingBean {    private SqlMapClient sqlMapClient;    protected SqlMapClient buildSqlMapClient(Resource configLocation,Properties properties) throws IOException {        InputStream is = configLocation.getInputStream();        if (properties != null) {            if (buildSqlMapClientWithInputStreamAndPropertiesMethodAvailable) {                return SqlMapClientBuilder.buildSqlMapClient(is,properties);            } else {                return SqlMapClientBuilder.buildSqlMapClient(new InputStreamReader(is), properties);            }        } else {            if (buildSqlMapClientWithInputStreamMethodAvailable) {                return SqlMapClientBuilder.buildSqlMapClient(is);            } else {                return SqlMapClientBuilder.buildSqlMapClient(new InputStreamReader(is));            }        }    }    //这里就是返回的、并会被注入到其它类里的对象    public Object getObject() {        return this.sqlMapClient;    }}


0 0
原创粉丝点击