前言
在通过原始方式使用 MyBatis 时,我们首先需要根据配置信息获得一个 SqlSessionFactory 对象:
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);//获得sqlSessionFactory对象
可以看到是通过 SqlSessionFactoryBuilder().build() 来得到的 SqlSessionFactory 对象
使用Spring
当使用 Spring 时,这个 SqlSessionFactory 对象应该交由 Spring 容器来管理。
而通常我们都是通过配置 SqlSessionFactoryBean 来向容器中注入 SqlSessionFactory 对象的
- 问题:为什么通过 SqlSessionFactoryBean 可以向 Spring 容器中注入 SqlSessionFactory ?
SqlSessionFactoryBean
public class SqlSessionFactoryBean
implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {
1、SqlSessionFactoryBean 实现了 FactoryBean 接口,且 “< SqlSessionFactory>”
说明:SqlSessionFactoryBean 是一个负责生产 SqlSessionFactory 的工厂 Bean, 当想从Spring容器中获取 SqlSessionFactory 对象时,Spring 会调用其 getObject() 方法进行获取。
2、SqlSessionFactoryBean 实现了 InitializingBean 接口
说明:在 SqlSessionFactoryBean 的生命周期中,初始化阶段会执行其 afterPropertiesSet() 方法
注:Bean的生命周期大致可分为三个阶段:(源码:AbstractAutowireCapableBeanFactory # doCreateBean)
1、创建Bean实例
2、属性赋值
3、初始化Bean( Aware接口回调 —> BeanPostProcessor前置处理 —> InitializingBean接口回调 —> BeanPostProcessor后置处理 )
向容器中注入 SqlSessionFactoryBean ,自然会经历 SqlSessionFactoryBean 的生命周期
属性赋值
在属性赋值阶段,Spring 通过读取 xml 中配置的 dataSource、configLocation… 等信息,调用 SqlSessionFactoryBean 中相应的 set 方法对其成员属性进行赋值(依赖注入)
- SqlSessionFactoryBean 常见的配置项:
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
<!--配置数据源-->
<property name="dataSource" ref="druidDataSource"/>
<!--配置MyBatis的主配置文件(可选)-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--配置mapper文件的路径(可选)-->
<property name="mapperLocations" value="classpath:mappers/*Mapper.xml"/>
<!--配置需要定义别名的实体类的包(可选)-->
<property name="typeAliasesPackage" value="com.springdemo.pojo"/>
</bean>
- 这里配置了 dataSource、configLocation… 四个属性
初始化:afterPropertiesSet()
之后进入初始化阶段,Spring 会调用其 afterPropertiesSet() 方法
@Override
public void afterPropertiesSet() throws Exception {
//...
this.sqlSessionFactory = buildSqlSessionFactory(); // 创建sqlSessionFactory对象赋值给成员属性
}
可以看到,内部通过 buildSqlSessionFactory() 方法创建了sqlSessionFactory对象,并赋值给了成员属性 this.sqlSessionFactory
buildSqlSessionFactory()方法:
这个方法的代码比较长,就不完整帖出来了。可以大致分析一下思路:
由代码的最后一句,可以发现底层还是通过 SqlSessionFactoryBuilder.build() 方法创建的 SqlSessionFactory 对象
方法依赖了一个 Configuration 对象:targetConfiguration,这个 targetConfiguration 对象中其实就包含了我们配置的 dataSource、configLocation…等信息
protected SqlSessionFactory buildSqlSessionFactory() throws Exception {
//...
return this.sqlSessionFactoryBuilder.build(targetConfiguration);
}
getObject()
方法很简单,就是返回成员属性 this.sqlSessionFactory ,作为 SqlSessionFactoryBean 生产的 SqlSessionFactory 对象
@Override
public SqlSessionFactory getObject() throws Exception {
if (this.sqlSessionFactory == null) {
afterPropertiesSet();
}
// 返回构造好的 sqlSessionFactory
return this.sqlSessionFactory;
}
总结
SqlSessionFactoryBean 是一个工厂Bean,在这个工厂Bean的生命周期历程中,完成了 SqlSessionFactory 对象的生产。
之后从容器中获取时,通过其 getObject() 方法就可以获取生产好的 SqlSessionFactory 对象。