spring mybatis之sqlSessionFactoryBean
2017-10-12 23:06
381 查看
sqlSessionFactoryBean实现了InitializingBean和FactoryBean
1.FactoryBean 实现了FactoryBean的类,通过getBean方法获取bean时,就是调用getObject()返回实例
2.InitializingBean 实现了InitializingBean的bean会在初始化时调用其afterPropertiesSet方法来进行bean的逻辑初始化
buildSqlSessionFactory
根据configLocation构造xmlConfigBuilder并进行解析,spring还整合了mybatis中其他属性的注入,并通过configuration来作为载体获取配置信息最终使用sqlSessionFactoryBuilder.build(configuration)得到sqlSessionFactory实例
2.getObject() 方法
1.FactoryBean 实现了FactoryBean的类,通过getBean方法获取bean时,就是调用getObject()返回实例
2.InitializingBean 实现了InitializingBean的bean会在初始化时调用其afterPropertiesSet方法来进行bean的逻辑初始化
public void afterPropertiesSet() throws Exception { notNull(dataSource, "Property 'dataSource' is required"); notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required"); this.sqlSessionFactory = buildSqlSessionFactory(); }
buildSqlSessionFactory
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath*:mybatis/mapper/**/*.xml" /> <property name="typeAliasesPackage" value="com.eroadsf.springdemo.mybatis.entity" /> </bean>
protected SqlSessionFactory buildSqlSessionFactory() throws IOException { Configuration configuration; XMLConfigBuilder xmlConfigBuilder = null; if (this.configLocation != null) { xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties); configuration = xmlConfigBuilder.getConfiguration(); } else { if (logger.isDebugEnabled()) { logger.debug("Property 'configLocation' not specified, using default MyBatis Configuration"); } configuration = new Configuration(); configuration.setVariables(this.configurationProperties); } if (this.objectFactory != null) { configuration.setObjectFactory(this.objectFactory); } if (this.objectWrapperFactory != null) { configuration.setObjectWrapperFactory(this.objectWrapperFactory); } //typeAliasesPackage if (hasLength(this.typeAliasesPackage)) { String[] typeAliasPackageArray = tokenizeToStringArray(this.typeAliasesPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); for (String packageToScan : typeAliasPackageArray) { configuration.getTypeAliasRegistry().registerAliases(packageToScan, typeAliasesSuperType == null ? Object.class : typeAliasesSuperType); if (logger.isDebugEnabled()) { logger.debug("Scanned package: '" + packageToScan + "' for aliases"); } } } //typeAliases if (!isEmpty(this.typeAliases)) { for (Class<?> typeAlias : this.typeAliases) { configuration.getTypeAliasRegistry().registerAlias(typeAlias); if (logger.isDebugEnabled()) { logger.debug("Registered type alias: '" + typeAlias + "'"); } } } //plugins if (!isEmpty(this.plugins)) { for (Interceptor plugin : this.plugins) { configuration.addInterceptor(plugin); if (logger.isDebugEnabled()) { logger.debug("Registered plugin: '" + plugin + "'"); } } } //typeHandlersPackage if (hasLength(this.typeHandlersPackage)) { String[] typeHandlersPackageArray = tokenizeToStringArray(this.typeHandlersPackage, ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); for (String packageToScan : typeHandlersPackageArray) { configuration.getTypeHandlerRegistry().register(packageToScan); if (logger.isDebugEnabled()) { logger.debug("Scanned package: '" + packageToScan + "' for type handlers"); } } } //typeHandlers if (!isEmpty(this.typeHandlers)) { for (TypeHandler<?> typeHandler : this.typeHandlers) { configuration.getTypeHandlerRegistry().register(typeHandler); if (logger.isDebugEnabled()) { logger.debug("Registered type handler: '" + typeHandler + "'"); } } } if (xmlConfigBuilder != null) { try { xmlConfigBuilder.parse(); if (logger.isDebugEnabled()) { logger.debug("Parsed configuration file: '" + this.configLocation + "'"); } } catch (Exception ex) { throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex); } finally { ErrorContext.instance().reset(); } } //transactionFactory if (this.transactionFactory == null) { this.transactionFactory = new SpringManagedTransactionFactory(); } Environment environment = new Environment(this.environment, this.transactionFactory, this.dataSource); configuration.setEnvironment(environment); //databaseIdProvider if (this.databaseIdProvider != null) { try { configuration.setDatabaseId(this.databaseIdProvider.getDatabaseId(this.dataSource)); } catch (SQLException e) { throw new NestedIOException("Failed getting a databaseId", e); } } //mapperLocations if (!isEmpty(this.mapperLocations)) { for (Resource mapperLocation : this.mapperLocations) { if (mapperLocation == null) { continue; } try { XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), configuration, mapperLocation.toString(), configuration.getSqlFragments()); xmlMapperBuilder.parse(); } catch (Exception e) { throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e); } 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构造xmlConfigBuilder并进行解析,spring还整合了mybatis中其他属性的注入,并通过configuration来作为载体获取配置信息最终使用sqlSessionFactoryBuilder.build(configuration)得到sqlSessionFactory实例
2.getObject() 方法
public SqlSessionFactory getObject() throws Exception { if (this.sqlSessionFactory == null) { afterPropertiesSet(); } return this.sqlSessionFactory; }
相关文章推荐
- MyBatis-Spring-SqlSessionFactoryBean
- MyBatis-Spring-SqlSessionFactoryBean
- Mybatis整合Spring SqlSessionFactoryBean的属性介绍
- 【spring+mybatis】MyBatis-Spring-SqlSessionFactoryBean
- MyBatis-Spring 学习笔记一 SqlSessionFactoryBean以及映射器类
- Mybatis整合Spring SqlSessionFactoryBean的属性介绍
- spring-mybatis 之SqlSessionFactoryBean
- spring +springmvc + mybatis 循环 DEBUG org.mybatis.spring.SqlSessionFactoryBean.RMI T
- MyBatis-Spring-SqlSessionFactoryBean
- mybatis与spring整合之错误定义sqlSessionFactoryBean
- myBatis3与spring整合之SqlSessionFactoryBean
- Spring-Mybatis --- 配置SqlSessionFactoryBean,整合Spring-Mybatis
- MyBatis-Spring-SqlSessionFactoryBean
- mybatis和spring整合报错:No bean named 'sqlSessionFactory' is defined
- Spring-MyBatis 之 SqlSessionFactoryBean
- 重构Mybatis与Spring集成的SqlSessionFactoryBean(1)
- MyBatis-Spring-SqlSessionFactoryBean
- MyBatis+Spring-SqlSessionFactoryBean
- MyBatis -- Spring -- SqlSessionFactoryBean
- Spring-mybatis整合配置错误,显示不能创建spring配置文件中的sqlSessionFactory的Bean