sqlSessionFactory创建

sqlSessionFactory创建前言分析上篇文章的整合的配置文件,我们可以知道配置的bean是成树状结构的,而在树的最顶层是类型为org.mybatis.Spring.SqlSessionFactoryBean的bean,它将其他相关的bean组装在了一起,那么我们的分析就从此类开始。sqlSessionFactory创建对于配置文件的读取,Spring是通过org.mybatis.Spring.SqlSessi…

大家好,又见面了,我是你们的朋友全栈君。

 前言

分析上篇文章的整合的配置文件,我们可以知道配置的bean是成树状结构的,而在树的最顶层是类型为org.mybatis.Spring.SqlSessionFactoryBean的bean,它将其他相关的bean组装在了一起,那么我们的分析就从此类开始。

sqlSessionFactory创建

 对于配置文件的读取,Spring是通过org.mybatis.Spring.SqlSessionFactoryBean封装了MyBatis中的实现。首先来看一下这个类的类图:

sqlSessionFactory创建

根据上图对两个感兴趣的接口FactoryBean和InitializingBean。

  ❤ InitializingBean:实现此接口的bean会在初始化时调用其afterPropertiesSet方法来进行bean的逻辑初始化。

  ❤ FactoryBean:一旦某个bean实现此接口,那么通过getBean方法获取bean时其实是获取此类的getObject返回的实例。

1.sqlSessionFactoryBean的初始化

 查看org.mybatis.Spring.SqlSessionFactoryBean类型的bean在初始化时做了哪些逻辑实现。

public void afterPropertiesSet() throws Exception {
        Assert.notNull(this.dataSource, "Property 'dataSource' is required");
        Assert.notNull(this.sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
        Assert.state(this.configuration == null && this.configLocation == null || this.configuration == null || this.configLocation == null, "Property 'configuration' and 'configLocation' can not specified with together");
        this.sqlSessionFactory = this.buildSqlSessionFactory();
    }

很显然,此函数的主要目的是对于sqlSessionFactory的初始化,sqlSessionFactory是所有MyBatis功能的基础。

protected SqlSessionFactory buildSqlSessionFactory() throws IOException {
        XMLConfigBuilder xmlConfigBuilder = null;
        Configuration configuration;
        if (this.configuration != null) {
            configuration = this.configuration;
            if (configuration.getVariables() == null) {
                configuration.setVariables(this.configurationProperties);
            } else if (this.configurationProperties != null) {
                configuration.getVariables().putAll(this.configurationProperties);
            }
        } else if (this.configLocation != null) {
            xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), (String)null, this.configurationProperties);
            configuration = xmlConfigBuilder.getConfiguration();
        } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration");
            }

            configuration = new Configuration();
            if (this.configurationProperties != null) {
                configuration.setVariables(this.configurationProperties);
            }
        }

        if (this.objectFactory != null) {
            configuration.setObjectFactory(this.objectFactory);
        }

        if (this.objectWrapperFactory != null) {
            configuration.setObjectWrapperFactory(this.objectWrapperFactory);
        }

        if (this.vfs != null) {
            configuration.setVfsImpl(this.vfs);
        }

        String[] typeHandlersPackageArray;
        String[] var4;
        int var5;
        int var6;
        String packageToScan;
        if (StringUtils.hasLength(this.typeAliasesPackage)) {
            typeHandlersPackageArray = StringUtils.tokenizeToStringArray(this.typeAliasesPackage, ",; \t\n");
            var4 = typeHandlersPackageArray;
            var5 = typeHandlersPackageArray.length;

            for(var6 = 0; var6 < var5; ++var6) {
                packageToScan = var4[var6];
                configuration.getTypeAliasRegistry().registerAliases(packageToScan, this.typeAliasesSuperType == null ? Object.class : this.typeAliasesSuperType);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases");
                }
            }
        }

        int var27;
        if (!ObjectUtils.isEmpty(this.typeAliases)) {
            Class[] var25 = this.typeAliases;
            var27 = var25.length;

            for(var5 = 0; var5 < var27; ++var5) {
                Class<?> typeAlias = var25[var5];
                configuration.getTypeAliasRegistry().registerAlias(typeAlias);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Registered type alias: '" + typeAlias + "'");
                }
            }
        }

        if (!ObjectUtils.isEmpty(this.plugins)) {
            Interceptor[] var26 = this.plugins;
            var27 = var26.length;

            for(var5 = 0; var5 < var27; ++var5) {
                Interceptor plugin = var26[var5];
                configuration.addInterceptor(plugin);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Registered plugin: '" + plugin + "'");
                }
            }
        }

        if (StringUtils.hasLength(this.typeHandlersPackage)) {
            typeHandlersPackageArray = StringUtils.tokenizeToStringArray(this.typeHandlersPackage, ",; \t\n");
            var4 = typeHandlersPackageArray;
            var5 = typeHandlersPackageArray.length;

            for(var6 = 0; var6 < var5; ++var6) {
                packageToScan = var4[var6];
                configuration.getTypeHandlerRegistry().register(packageToScan);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers");
                }
            }
        }

        if (!ObjectUtils.isEmpty(this.typeHandlers)) {
            TypeHandler[] var28 = this.typeHandlers;
            var27 = var28.length;

            for(var5 = 0; var5 < var27; ++var5) {
                TypeHandler<?> typeHandler = var28[var5];
                configuration.getTypeHandlerRegistry().register(typeHandler);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Registered type handler: '" + typeHandler + "'");
                }
            }
        }

        if (this.databaseIdProvider != null) {
            try {
                configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource));
            } catch (SQLException var24) {
                throw new NestedIOException("Failed getting a databaseId", var24);
            }
        }

        if (this.cache != null) {
            configuration.addCache(this.cache);
        }

        if (xmlConfigBuilder != null) {
            try {
                xmlConfigBuilder.parse();
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'");
                }
            } catch (Exception var22) {
                throw new NestedIOException("Failed to parse config resource: " + this.configLocation, var22);
            } finally {
                ErrorContext.instance().reset();
            }
        }

        if (this.transactionFactory == null) {
            this.transactionFactory = new SpringManagedTransactionFactory();
        }

        configuration.setEnvironment(new Environment(this.environment, this.transactionFactory, this.dataSource));
        if (!ObjectUtils.isEmpty(this.mapperLocations)) {
            Resource[] var29 = this.mapperLocations;
            var27 = var29.length;

            for(var5 = 0; var5 < var27; ++var5) {
                Resource mapperLocation = var29[var5];
                if (mapperLocation != null) {
                    try {
                        XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), configuration, mapperLocation.toString(), configuration.getSqlFragments());
                        xmlMapperBuilder.parse();
                    } catch (Exception var20) {
                        throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", var20);
                    } finally {
                        ErrorContext.instance().reset();
                    }

                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'");
                    }
                }
            }
        } else if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found");
        }

        return this.sqlSessionFactoryBuilder.build(configuration);
    }

从函数中可以看到,尽管我们还是习惯于将MyBatis的配置和Spring的配置独立出来,但是,这并不代表Spring中的配置不支持直接配置。也就是说,在上篇文章的示例中,完全裤取消配置中的configLocation属性,而把其中的属性直接写在sqlSessionFactoryBean中。

从这个函数中还可以得知,配置文件还可以支持其他多种属性的配置,如configLocation、objectFactory、objectWrapperFactory、typeAliasesPackage、typeAliases、typeHandlersPackage、plugins、typeHandlers、transactionFactory、databaseIdProvider、mapperLocations。

其实,如果只按照常用的配置,那么我们只需要在函数最开始按照如下方式处理configLocation:

   xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(),null,this.configurationProperties);
    configuration = xmlConfigBuilder.getConfiguration();

 根据configLocation构造XMLConfigBuilder并进行解析,但是,为了体现Spring更强大的兼容性,Spring还整合了MyBatis中其他属性的注入,并通过实例configuration来承载每一步所获取的信息并最终使用sqlSessionFactoryBuilder实例根据解析到的configuration创建sqlSessionFactory实例。

2.获取SqlSessionFactoryBean实例

 由于sqlSessionFactoryBean实现了FactoryBean接口,所以当通过getBean方法获取对应实例时,其实是获取该类的getObject函数返回的实例,也就是获得初始化后的sqlSessionFactory属性。

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

        return this.sqlSessionFactory;
    }

 参考:《Spring源码深度解析》 郝佳 编著: 

转载于:https://www.cnblogs.com/Joe-Go/p/10255301.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/140666.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 液晶显示器测试软件6,屏幕坏点检测工具(Datum pixel repair)

    一般有很多的朋友都是不知道自己的电脑的屏幕是不是有多少个坏点的,或者是不知道怎么去测试,那么现在小编就给你介绍下面的这款屏幕坏点检测工具(Datumpixelrepair)来帮助你测试吧!Datumpixelrepair介绍Datumpixelrepair是一款可以快速检测出液晶显示器坏点的位置,用户点击不同的颜色填充整个屏幕,颜色包括:白、黑、红、绿等,使用户更容易检测出坏点!屏幕坏…

    2022年4月7日
    637
  • idea激活码2021[在线序列号]

    idea激活码2021[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月18日
    58
  • 查看Tomcat进程_windows查看tomcat进程

    查看Tomcat进程_windows查看tomcat进程jps|grepBootstrappsaux|grepcatalina.startup.bootstrappsaux|greptomcatpsaux|grep8080按照端口号查看lsof-i:8080 

    2022年9月20日
    2
  • 程序化交易编程学习_C语言 教程

    程序化交易编程学习_C语言 教程在交易的过程当中,应用程序化交易的都知道,最困难的就是如何构建出一个交易策略,交易策略的构建过程是非常复杂的,一个完整的程序化交易策略是由很多的相关和独立的步骤组合而成的,同时要把每一个步骤都落实好和在研发的过程中,对于从下个步骤中得到的资讯,要利用它调整和加工上一个步骤,只有这样才能获得一个相对完善的交易策略。接下来,小编和大家分享一下研发交易策略的步骤及具体说明,希望对大家的交易策略有所帮助:…

    2022年10月8日
    3
  • Java学习之面向对象篇

    Java学习之面向对象篇0X00前言前面讲了java的一些基本语法,这里就来讲讲java的核心思想,面向对象。0X01面向对象概念Java语言是一种面向对象的程序设计语言,而面向对象思想是

    2021年12月11日
    71
  • Python入门习题(40)——CCF CSP认证考试真题:报数游戏「建议收藏」

    Python入门习题(40)——CCF CSP认证考试真题:报数游戏「建议收藏」CCFCSP认证考试真题(201712-2):游戏问题描述解题思路参考答案测试用例小结问题描述试题编号: 201712-2试题名称: 游戏时间限制: 1.0s内存限制: 256.0MB问题描述  有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐在1号小朋友的顺时针方向,3号小朋友坐在2号小朋友的顺时针方向,……,1号小朋友坐在n号小朋友的顺时针方向。  游戏开始,从1…

    2025年9月1日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号