Mybatis源码分析(二):源码分析入口引导

版权声明:欢迎转载,注明出处即可,谢谢! https://blog.csdn.net/qq_17231297/article/details/80176082

如果没看Mybatis源码分析(一):源码准备工作,再看本节


1. 引导进入spring的源码


首先是mybatis对  xml文件 进行解析,那么这个解析的关键配置就是


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="mapperLocations">

   <value>classpath*:com/zzy/xml/*Mapper.xml</value>

</property>

</bean>

 

以上配置org.mybatis.spring.SqlSessionFactoryBean这个类是解析xml文件的核心类,这个类中有很多属性,也就是property其中mapperLocationsxml文件扫描的属性类引用,其他属性在源码中进一步分析!

 

如果您使用的是eclipse,那么Ctrl+鼠标左键【提前是你已经下载了mybatis相关源码】进入SqlSessionFactoryBean如下图:

 

这个类实现了InitializingBean接口


题外话:

这个核心类在mybatis那个jar中呢,如下图:

可以看到在mybatis-spring这个jar的最外层!


2. IntializingBean这个接口做简单说明


这个类后期还会在spring源码分析中细讲这里只说明这个类的作用

 

作用:spring中bean实例化和依赖注入完成以后,做一些后续的初始化工作,实现IntializingBean这个接口,必须重写其中的afterPropertiesSet这个方法!如下图简单看看这个接口的内容:

3. 回到sqlSessionFactory类中看看其afterPropertiesSet中做哪些事情呢?


回到spring的源码分析上,上文说了SqlSessionFactoryBean实现了InitializingBeanafterPropertiesSet我们在SqlSessionFactoryBean中找下这个方法,Ctrl+O:

 

点击调到该方法

其中我们需要关注的代码段是:


 this.sqlSessionFactory = buildSqlSessionFactory();

 

 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");

 

这些都是对必须配置属性的异常处理。

 

其中buildSqlSessionFactory()这个方法是我们研究的核心!

 

4. SqlSessionFactoryBean有哪些属性呢?


SqlSessionFactoryBean属性也就是


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />

<property name="mapperLocations">

   <value>classpath*:com/zzy/xml/*Mapper.xml</value>

</property>

</bean>


下可配置的property 有哪些呢?如下源码(其中mapperLocations dataSource经常用到的,其他有些事ibatis原有,为了保持兼容性,很多基本不用了)下节对常用属性做简单介绍


   private Resource configLocation;

 

  private Configuration configuration;

 

  private Resource[] mapperLocations;

 

  private DataSource dataSource;

 

  private TransactionFactory transactionFactory;


  private Properties configurationProperties;

 

  private SqlSessionFactoryBuilder sqlSessionFactoryBuilder =   new SqlSessionFactoryBuilder();

 

  private SqlSessionFactory sqlSessionFactory;

 

  //EnvironmentAware requires spring 3.1

  private String environment =    SqlSessionFactoryBean.class.getSimpleName();

 

  private boolean failFast;

 

  private Interceptor[] plugins;

 

  private TypeHandler<?>[] typeHandlers;

 

  private String typeHandlersPackage;

 

  private Class<?>[] typeAliases;

 

  private String typeAliasesPackage;

 

  private Class<?> typeAliasesSuperType;

 

  //issue #19. No default provider.

  private DatabaseIdProvider databaseIdProvider;

 

  private Class<? extends VFS> vfs;

 

  private Cache cache;

 

  private ObjectFactory objectFactory;

 

  private ObjectWrapperFactory objectWrapperFactory;


以上配置的属性最终都会被3点buildSqlSessionFactory()方法读取到

,然后对相关属性的配置处理!截图简单看看,怎么处理这些属性呢?举例看下mapperLocations dataSource

以上截图来自buildSqlSessionFactory这个方法,下节源码分析该方法以及相关配置属性的说明!

                                                                                   更多精彩请扫码关注微信公众号

                                                                                           名称:java版web项目  

                                                                                            id :java_project

猜你喜欢

转载自blog.csdn.net/qq_17231297/article/details/80176082
今日推荐