spring的LocalSessionFactoryBean生成过程与hibernate的SessionFactory生成过程是高度吻合的

为了后面源码分析,首先讲解一个接口,一个类的功能:
①、接口InitializingBean
接口的功能:这个接口专门为bean设计的,它只有一个方法。我们知道所有的bean都是由beanFactory来生成的,如果一个bean实现了该接口,在beanFactory为该bean装配好了所有的属性以后,在返回实际bean之前还会调用一次该接口的afterPropertiesSet(...)方法。其设计目的是为了实现个性化,或者是为了检查bean属性值的完整性等。

public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}

②、类LocalSessionFactoryBuilder

显然,LocalSessionFactoryBuilder继承自org.hibernate.cfg.Configuration,那么Configuration拥有的属性,LocalSessionFactoryBuilder也具有

public class LocalSessionFactoryBuilder extends Configuration{
//...
}

从LocalSessionFactoryBean源码中分析出其于hibernate的sessionFactory和configuration之间的关系:

public interface InitializingBean {
void afterPropertiesSet() throws Exception;
} //②、类LocalSessionFactoryBuilder
//显然,LocalSessionFactoryBuilder继承自org.hibernate.cfg.Configuration,那么Configuration拥有的属性,LocalSessionFactoryBuilder也具有
public class LocalSessionFactoryBuilder extends Configuration{
//...
} //现在主要分析LocalSessionFactoryBean
//1、看一下几个非常重要的属性值定义
public class LocalSessionFactoryBean extends... implements InitializingBean, ...{
//数据源
private DataSource dataSource;
//hibernate的配置文件Xxx.cfg.xml所在的
//位置多个可以用","隔开
private Resource[] configLocations;
private String[] mappingResources;
//hibernate的映射文件位置
private Resource[] mappingLocations;
//hibernate的properties属性,存放了配置
//文件中解析property标签的结果
private Properties hibernateProperties;
//hibernate的configuration属性
private Configuration configuration;
//这个sessionfactory是hibernate的sessionFactory
private SessionFactory sessionFactory; //省略其它的属性以及setter方法... //注意这个set方法,说明当只有一个配置文件
//的时候可以使用configLocation属性来配置,
//最终也会被转换成configLocations
public void setConfigLocation(Resource configLocation) {
this.configLocations = new Resource[] {configLocation};
} @Override
public void afterPropertiesSet() throws IOException {
LocalSessionFactoryBuilder sfb = new LocalSessionFactoryBuilder(this.dataSource, this.resourcePatternResolver); if (this.configLocations != null) {
for (Resource resource : this.configLocations) {
//sfb.configure(...)实际上也就是调用其父类
//org.hibernate.cfg.Configuration的configure(...)方法
//这里完成xxx.cfg.xml文件中property节点
//的解析,得到一个properties
sfb.configure(resource.getURL());
}
} //mappingResources和mappingLocations效果是一样的,
//都会将资源转换成输入流,并调用sfb.addInputStream(...)方法
//sfb.addInputStream(...)最终会完成xxx.cfg.xml文件的
//非property节点解析(主要有3类:mapping、class-cache和collection-cache)
//将解析结果放入到metadataSourceQueue中
if (this.mappingResources != null) {
for (String mapping : this.mappingResources) {
Resource mr = new ClassPathResource(mapping.trim(), this.resourcePatternResolver.getClassLoader());
sfb.addInputStream(mr.getInputStream());
}
}
if (this.mappingLocations != null) {
for (Resource resource : this.mappingLocations) {
sfb.addInputStream(resource.getInputStream());
}
} //sfb.addProperties(...)方法会调用properties.putAll( extraProperties )方法
//说明我们可以配置一个properties对象来达到配置xxx.cfg.xml相同的效果!!
//两方面的配置最终都会放入到properties对象中
if (this.hibernateProperties != null) {
sfb.addProperties(this.hibernateProperties);
} //省略若干其它方法... // 将sfb向上转型,LocalSessionFactoryBean中的configuration属性实际上
// 就是org.hibernate.cfg.Configuration
this.configuration = sfb;
//在得到configuration以后,通过它来创建一个sessionFactory,
//并赋值给sessionFactory属性(LocalSessionFactoryBean的属性)
//在底层会调用return super.buildSessionFactory()来的到一个
//sessionFactory,由于其父类是org.hibernate.cfg.Configuration
//所以,相当于调用了configuration.buildSessionFactory()来生成
//sessionFactory,这是hibernate4.0版本之前的做法,新版本已经
//被buildSessionFactory(ServiceRegistry)所取代。
this.sessionFactory = buildSessionFactory(sfb);
}
}

总结spring的LocalSessionFactoryBean实际完成的工作有:
1、通过解析bean中的configLocation和mappingLocations等属性,得到一个hibernate的原生态的org.hibernate.cfg.Configuration属性
2、通过org.hibernate.cfg.Configuration生成一个hibernate原生态的org.hibernate.SessionFactory属性
3、可以在外部配置一个Properties对象,并将其配置为properties属性,可以达到与xxx.cfg.xml相同的配置效果

2、Spring的LocalSessionFactoryBean创建过程源码分析的更多相关文章

  1. springboot 事务创建流程源码分析

    springboot 事务创建流程源码分析 目录 springboot 事务创建流程源码分析 1. 自动加载配置 2. InfrastructureAdvisorAutoProxyCreator类 3 ...

  2. spring mvc之启动过程源码分析

    简介 这两个星期都在看spring mvc源码,看来看去还是还是很多细节没了解清楚,在这里把看明白的记录下,欢迎在评论中一起讨论. 一.铺垫 spring mvc是基于servlet的,在正式分析之前 ...

  3. 【Spring boot】启动过程源码分析

    启动过程结论 推测web应用类型. spi的方式获取BootstrapRegistryInitializer.ApplicationContextInitializer.ApplicationCont ...

  4. spring mvc之请求过程源码分析

    简介 上一篇,我们分析了spring mvc启动过程的源码,这一节,来一起分析下在用户请求controller的过程中,spring mvc做了什么事? 一.准备 我写这么一个controller p ...

  5. linux内核中socket的创建过程源码分析(总结性质)

    在漫长地分析完socket的创建源码后,发现一片浆糊,所以特此总结,我的博客中同时有另外一篇详细的源码分析,内核版本为3.9,建议在阅读本文后若还有兴趣再去看另外一篇博文.绝对不要单独看另外一篇. 一 ...

  6. spring启动加载过程源码分析

    我们知道启动spring容器两常见的两种方式(其实都是加载spring容器的xml配置文件时启动的): 1.在应用程序下加载 ApplicationContext ctx = new ClassPat ...

  7. Spring Cloud学习 之 Spring Cloud Ribbon(执行流程源码分析)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 分析: 总结: 分析: ​ 在上篇文章中,我们着重分析了RestTempla ...

  8. Spring IOC容器核心流程源码分析

    简单介绍 Spring IOC的核心方法就在于refresh方法,这个方法里面完成了Spring的初始化.准备bean.实例化bean和扩展功能的实现. 这个方法的作用是什么? 它是如何完成这些功能的 ...

  9. linux内核中socket的创建过程源码分析(详细分析)

    1三个相关数据结构. 关于socket的创建,首先需要分析socket这个结构体,这是整个的核心. 104 struct socket { 105         socket_state       ...

随机推荐

  1. zabbix语言设置

    1.怎样支持中文: https://www.zabbix.org/wiki/How_to/install_locale 官方解决方法 实际操作中,进入/var/lib/locales/supporte ...

  2. 浏览器不支持HTML5

    有些浏览器并不支持HTML5中的新增元素,如IE8或更早版本.想要应用样式,可以头部标记<head>中加入下面JavaScript代码 <html> <head> ...

  3. 重装系统必做之——更换Windows系统的默认临时文件的存储目录

    作为一名计算机爱好者,重装电脑是家常便饭,但是重装电脑的目的无非就是: 1.操作系统更新换代: 2.系统速度太慢: 或者更多.... 我们大多数目的都是上述中第2点,有时候是否仅仅重装系统而忽略了一些 ...

  4. ZeroMQ 在 centos 6.5_x86_64 下的安装

    ZeroMQ 在 centos 6.5_x86_64 下的安装 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.ZeroMQ介绍 ZeroMQ是一个开 ...

  5. [noip2010]关押罪犯 并查集

    第一次看的时候想到了并查集,但是不知道怎么实现: 标解,f[i]表示i所属的集合,用f[i+n]表示i所属集合的补集,实现的很巧妙,可以当成一个使用并查集的巧妙应用: #include<iost ...

  6. while小问题

    while(!m_SMque.pop(data)); 看到这个有点忘了,如果pop返回false会一直执行pop,其实这个执行的是空语句,而while每次执行都需要判断条件,所以如果pop返回fals ...

  7. [百度空间] [转]DLL地狱及其解决方案

    DLL地狱及其解决方案 原作者:Ivan S Zapreev 译者:陆其明概要 本文将要介绍DLL的向后兼容性问题,也就是著名的“DLL Hell”问题.首先我会列出自己的研究结果,其中包括其它一些研 ...

  8. Linux 配置网络

    1.vi  /etc/sysconfig/network-scripts/ifcfg-eth0 2. # Advanced Micro Devices [AMD] 79c970 [PCnet32 LA ...

  9. C/C++中内存区域划分大总结

    C++作为一款C语言的升级版本,具有非常强大的功能.它不但能够支持各种程序设计风格,而且还具有C语言的所有功能.我们在这里为大家介绍的是其中一个比较重要的内容,C和C++内存区域的划分. 一. 在c中 ...

  10. CRF++中文分词使用指南

    http://blog.csdn.net/marising/article/details/5769653 前段时间写了中文分词的一些记录里面提到了CRF的分词方法,近段时间又研究了一下,特把方法写下 ...