一、sqlSessionFactory创建
通过配置文件我们分析,对于配置文件的读取解析,spring应该通过org.mybatis.Spring.SqlSessionFactoryBean封装了Mybatis中的实现。可以找到我们感兴趣的两个接口,FactoryBean和InitializingBean。
InitializingBean:实现此接口的bean会在初始化时调用其afterPropertiesSet方法进行bean的逻辑初始化。
FactoryBean:一旦某个bean实现此接口,那么通过getBean方法获取bean时其实是获取此类的getObject()返回的实例。
我们首先以InitializingBean接口的afterPropertiesSet()方法作为突破点。
1、SqlSessionFactoryBean的初始化。
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的初始化,我们知道SqSessionFactory是所有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); }
我们可以取消配置中的configLocation属性,而把其中的属性直接写在SqlSessionFactoryBean中。
<!--声明的是mybatis中提供的SqlSessionFactoryBean类,这个类内部创建SqlSessionFactory的--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--set注入,把数据库连接池赋给了dataSource属性--> <property name="dataSource" ref="myDataSource"/> <!--mybatis主配置文件的位置 configLocation的属性是Resource类型,读取属性配置文件 可查看源码一探究竟 它的赋值,使用value,指定文件的路径,使用classpath:表示文件的位置,这个是规定的。 当spring执行这句话的时候,就会把SqlSessionFactory这个对象创建出来! --> <property name="configLocation" value="classpath:mybatis.xml"/> </bean>
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; }
二、MapperFactoryBean的创建
为了使用Mybatis功能,实例中的Spring配置文件提供了两个bean,除了之前分析的SqlSessionFactoryBean类型以外,还有一个MapperFactoryBean类型的bean。spring使用mybatis使用数据库接口的方式是:
StudentService service = (StudentService) ctx.getBean("studentService");
Spring中获取的名为service的bean,其实是与单独使用Mybatis完成了一样的功能,因此我们推断出,在bean的创建过程中一定使用了Mybatis中原生方法sqlSession.getMapper(studentService.class)进行了一次封装。结合配置文件,我们把目标转向org.mybatis.Spring.mapper.MapperFactoryBean,初步推测其中的逻辑应该在此类中实现。我们通过查看MapperFactoryBean的类层次结构图发现:
在实现的接中发现了我们感兴趣的两个接口InitializingBean与FactoryBean。
我们依旧是从bean的初始化开始。
1、MapperFactoryBean的初始化。
通过不断追踪父类源码发现afterPropertieSet方法是在DaoSupport类中实现,代码如下:
public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException { this.checkDaoConfig(); try { this.initDao(); } catch (Exception var2) { throw new BeanInitializationException("Initialization of DAO failed", var2); } }
而后观察代码发现,MapperFactoryBean的初始化包括对Dao的配置以及初始化,其中intiDao()是模板方法,设计为留给子类做出进一步逻辑处理。而checkDaoConfig()才是我们分析的重点。
protected void checkDaoConfig() { super.checkDaoConfig(); Assert.notNull(this.mapperInterface, "Property 'mapperInterface' is required"); Configuration configuration = this.getSqlSession().getConfiguration(); if (this.addToConfig && !configuration.hasMapper(this.mapperInterface)) { try { configuration.addMapper(this.mapperInterface); } catch (Exception var6) { this.logger.error("Error while adding the mapper '" + this.mapperInterface + "' to configuration.", var6); throw new IllegalArgumentException(var6); } finally { ErrorContext.instance().reset(); } } }
super.checkDaoConfig()在SqlSessionDaoSupport类实现,代码如下:
protected void checkDaoConfig() { Assert.notNull(this.sqlSession, "Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required"); }
结合代码我们了解到对于DAO配置的验证,Spring做了以下几个方面的工作:
sqlSession作为根据接口创建映射器代理类一定不可以为空,而sqlSession的初始化工作是在设定其sqlSessionFactory属性时完成的。
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { if (!this.externalSqlSession) { this.sqlSession = new SqlSessionTemplate(sqlSessionFactory); } }
也就是说,如果忽略了对于sqlSessionFactory属性的设置,此时就会检测出来。
2、获取MapperFactoryBean的实例
由于MapperFactoryBean实现了FactoryBean接口,所以当通过getBean方法获取实例时,其实是获取该类的getObject()函数返回的实例。
public T getObject() throws Exception { return this.getSqlSession().getMapper(this.mapperInterface); }
这段代码正是我们在提供Mybatis独立使用的时候的一个代码调用。Spring通过FactoryBean进行了封装。
Spring整合mybatis(源码分析)
最新推荐文章于 2024-09-26 19:24:27 发布