二、SqlSessionFactoryBean和MapperFactoryBean作用

SqlSessionFactoryBean

SqlSessionFactoryBean實(shí)現(xiàn)了FactoryBean接口,在創(chuàng)建bean時(shí)會(huì)調(diào)用getObject方法。

@Override
public SqlSessionFactory getObject() throws Exception {
if (this.sqlSessionFactory == null) {
    afterPropertiesSet();
}

return this.sqlSessionFactory;
}

如果sqlSessionFactory對(duì)象為空,則調(diào)用 afterPropertiesSet()方法,因?yàn)镾qlSessionFactoryBean還實(shí)現(xiàn)了InitializingBean接口,在bean創(chuàng)建的時(shí)候就會(huì)調(diào)用afterPropertiesSet()方法。所以這步this.sqlSessionFactory == null只是增加個(gè)校驗(yàn)。

@Override
public void afterPropertiesSet() throws Exception {
notNull(dataSource, "Property 'dataSource' is required");
notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
            "Property 'configuration' and 'configLocation' can not specified with together");

this.sqlSessionFactory = buildSqlSessionFactory();
}

afterPropertiesSet方法調(diào)用buildSqlSessionFactory方法創(chuàng)建sqlSessionFactory對(duì)象。buildSqlSessionFactory方法很長(zhǎng),不過(guò)主要是給Configuration對(duì)象設(shè)置屬性值,這里就不貼代碼了。

MapperFactoryBean

MapperFactoryBean是用來(lái)創(chuàng)建MyBatis Mapper對(duì)象的,MapperFactoryBean也實(shí)現(xiàn)了FactoryBean接口,間接實(shí)現(xiàn)InitializingBean接口,我們先看下InitializingBean接口的afterPropertiesSet實(shí)現(xiàn)。

@Override
public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException {
    // Let abstract subclasses check their configuration.
    checkDaoConfig();

    // Let concrete implementations initialize themselves.
    try {
        //空實(shí)現(xiàn)
        initDao();
    }
    catch (Exception ex) {
        throw new BeanInitializationException("Initialization of DAO failed", ex);
    }
}

checkDaoConfig方法由MapperFactoryBean實(shí)現(xiàn)。主要是把Mapper接口添加到Configuration對(duì)象中。

@Override
protected void checkDaoConfig() {
    super.checkDaoConfig();

    notNull(this.mapperInterface, "Property 'mapperInterface' is required");
    //如果Configuration中不存在Mapper,則添加Mapper
    Configuration configuration = getSqlSession().getConfiguration();
    if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) {
        try {
        configuration.addMapper(this.mapperInterface);
        } catch (Exception e) {
        logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", e);
        throw new IllegalArgumentException(e);
        } finally {
        ErrorContext.instance().reset();
        }
    }
}

接下來(lái)我們?cè)倏碝apperFactoryBean中的getObject方法。

@Override
public T getObject() throws Exception {
    return getSqlSession().getMapper(this.mapperInterface);
}

getObject方法返回Mapper對(duì)象,Mapper對(duì)象是從Configuration中獲取。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容