2、Spring的LocalSessionFactoryBean创建过程源码分析
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创建过程源码分析的更多相关文章
- springboot 事务创建流程源码分析
springboot 事务创建流程源码分析 目录 springboot 事务创建流程源码分析 1. 自动加载配置 2. InfrastructureAdvisorAutoProxyCreator类 3 ...
- spring mvc之启动过程源码分析
简介 这两个星期都在看spring mvc源码,看来看去还是还是很多细节没了解清楚,在这里把看明白的记录下,欢迎在评论中一起讨论. 一.铺垫 spring mvc是基于servlet的,在正式分析之前 ...
- 【Spring boot】启动过程源码分析
启动过程结论 推测web应用类型. spi的方式获取BootstrapRegistryInitializer.ApplicationContextInitializer.ApplicationCont ...
- spring mvc之请求过程源码分析
简介 上一篇,我们分析了spring mvc启动过程的源码,这一节,来一起分析下在用户请求controller的过程中,spring mvc做了什么事? 一.准备 我写这么一个controller p ...
- linux内核中socket的创建过程源码分析(总结性质)
在漫长地分析完socket的创建源码后,发现一片浆糊,所以特此总结,我的博客中同时有另外一篇详细的源码分析,内核版本为3.9,建议在阅读本文后若还有兴趣再去看另外一篇博文.绝对不要单独看另外一篇. 一 ...
- spring启动加载过程源码分析
我们知道启动spring容器两常见的两种方式(其实都是加载spring容器的xml配置文件时启动的): 1.在应用程序下加载 ApplicationContext ctx = new ClassPat ...
- Spring Cloud学习 之 Spring Cloud Ribbon(执行流程源码分析)
Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 分析: 总结: 分析: 在上篇文章中,我们着重分析了RestTempla ...
- Spring IOC容器核心流程源码分析
简单介绍 Spring IOC的核心方法就在于refresh方法,这个方法里面完成了Spring的初始化.准备bean.实例化bean和扩展功能的实现. 这个方法的作用是什么? 它是如何完成这些功能的 ...
- linux内核中socket的创建过程源码分析(详细分析)
1三个相关数据结构. 关于socket的创建,首先需要分析socket这个结构体,这是整个的核心. 104 struct socket { 105 socket_state ...
随机推荐
- 双系统修改启动项顺序&&&修改开机启动等待时间
1. 双系统修改启动项顺序 更改/etc/grub.d目录 下的文件名是可行的 默认情况下Windows 7对应的文件名是30_os-prober,第一个linux系统对应的是10- ...
- unity 协同
void Update () { if(Input .GetKeyDown (KeyCode .W )) { StartCoroutine ("Test"); } } IEnume ...
- MySQL中求年龄
时间函数: 1.curdate() --- 当前系统日期 调取: select curdate() 2.curtime() --- 当前系统时间 调取: select curtime() 3.now( ...
- WebServices中使用Session
默认情况下,Asp.net使用cookie来管理会话状态.因此,Asp.net假设客户端存储了会话cookie并将它与每一个请求一并发回给客户端. /// <summary> /// Su ...
- JS 学习笔记--2--变量的声明
1.ECMAScript 中规定所有的关键字.保留字.函数名.函数名.操作符等都是区分大小写的. 2.标识符:指变量.函数.属性的名字:标识符组成:以字母.下划线.$ 开头,其他字母可以含有数字, ...
- 剑指offer-17题
题目要求:输入一个表示整数的字符串,把该字符串转换成整数并输出.例如输入字符串"345",则输出整数345. 分析:这道题能够很好地反应出程序员的思维和编程习惯. 的确,自己编写的 ...
- openOffice将doc在线预览
最近,有个项目要用到类似DOCIN的文档转换和阅读的功能,于是就开始找相关的资料,最后总结出2种解决办法,以下就来探讨下两种方法的各自实现. 第一种:通过FLASH PAPER来转换DOC文档直接生成 ...
- Win32 Plus Extra Height of Caption Bar
you set the size of the non-client area by handling the WM_NCCALCSIZE message. But don't do this unl ...
- PE文件之资源讲解
资源是PE文件中非常重要的部分,几乎所有的PE文件中都包含资源,与导入表与导出表相比,资源的组织方式要复杂得多,要了解资源的话,重点在于了解资源整体上的组织结构. 我们知道,PE文件资源中的内容包括: ...
- 2014 ACM/ICPC Asia Regional Xi'an Online Paint Pearls
传说的SB DP: 题目 Problem Description Lee has a string of n pearls. In the beginning, all the pearls have ...