Hibernate_hibernate.cfg.xml加载流程源码分析
来源:互联网 发布:诺基亚软件下载专区 编辑:程序博客网 时间:2024/06/06 03:06
在使用hibernate的时候,需要创建一个configuration对象来加载xml文件
Configuration configuration = new Configuration();configuration.configure();我们来跟一下configure方法
public Configuration configure() throws HibernateException {configure( "/hibernate.cfg.xml" );return this;}
public Configuration configure(String resource) throws HibernateException {LOG.configuringFromResource( resource );InputStream stream = getConfigurationInputStream( resource );return doConfigure( stream, resource );}
有两个方法,一个没有参数,一个有参数,可以发现,其实没有参数的configure方法也是调用了有参数的configure方法,参数为"hibernate.cfg.xml",所以,如果你调用了没有参数的configure方法,那么就要把xml文件放到src下面,否则就要把xml文件的目录作为参数传进去
不管LOG,这里有两个方法被执行了
1.InputStream stream = getConfigurationInputStream(resource);
protected InputStream getConfigurationInputStream(String resource) throws HibernateException {LOG.configurationResource( resource );return ConfigHelper.getResourceAsStream( resource );}继续看ConfigHelper.getResourceAsStream(resource);
public static InputStream getResourceAsStream(String resource) {String stripped = resource.startsWith("/") ?resource.substring(1) : resource;InputStream stream = null;ClassLoader classLoader = ClassLoaderHelper.getContextClassLoader();if (classLoader!=null) {stream = classLoader.getResourceAsStream( stripped );}if ( stream == null ) {stream = Environment.class.getResourceAsStream( resource );}if ( stream == null ) {stream = Environment.class.getClassLoader().getResourceAsStream( stripped );}if ( stream == null ) {throw new HibernateException( resource + " not found" );}return stream;}到了这里,就获取了xml的stream
2.doConfigure( stream, resource );
protected Configuration doConfigure(InputStream stream, String resourceName) throws HibernateException {try {ErrorLogger errorLogger = new ErrorLogger( resourceName );Document document = xmlHelper.createSAXReader( errorLogger, entityResolver ).read( new InputSource( stream ) );if ( errorLogger.hasErrors() ) {throw new MappingException( "invalid configuration", errorLogger.getErrors().get( 0 ) );}doConfigure( document );}catch (DocumentException e) {throw new HibernateException( "Could not parse configuration: " + resourceName, e );}finally {try {stream.close();}catch (IOException ioe) {LOG.unableToCloseInputStreamForResource( resourceName, ioe );}}return this;}
可以看到,使用SAX解析了xml文件,然后执行了doConfigure(document);
看一下doConfigure方法
protected Configuration doConfigure(Document doc) throws HibernateException {Element sfNode = doc.getRootElement().element( "session-factory" );String name = sfNode.attributeValue( "name" );if ( name != null ) {properties.setProperty( Environment.SESSION_FACTORY_NAME, name );}addProperties( sfNode );parseSessionFactory( sfNode, name );Element secNode = doc.getRootElement().element( "security" );if ( secNode != null ) {parseSecurity( secNode );}LOG.configuredSessionFactory( name );LOG.debugf( "Properties: %s", properties );return this;}
到了这里,我们可以看到,sfNode获得了xml中<session-factory>节点的内容,然后执行了addProperties(sfnode)
private void addProperties(Element parent) {Iterator itr = parent.elementIterator( "property" );while ( itr.hasNext() ) {Element node = (Element) itr.next();String name = node.attributeValue( "name" );String value = node.getText().trim();LOG.debugf( "%s=%s", name, value );properties.setProperty( name, value );if ( !name.startsWith( "hibernate" ) ) {properties.setProperty( "hibernate." + name, value );}}Environment.verifyProperties( properties );}
通过迭代之后,把所有属性加载完毕。
0 0
- Hibernate_hibernate.cfg.xml加载流程源码分析
- Hibernate_hibernate.cfg.xml
- Hibernate源码解析---------hibernate.cfg.xml读取流程
- WordPress源码分析:首页加载流程
- Android平台类加载流程源码分析
- Android 6.0 View加载流程源码分析
- Android Launcher加载流程源码分析
- Dubbo SPI组件加载流程源码分析
- android中jni加载流程源码分析
- require.js源码分析之加载流程
- View的加载流程源码分析
- hibernate.cfg.xml配置文件分析
- hibernate.cfg.xml配置文件分析
- hibernate.cfg.xml配置文件分析
- Duilib 源码分析之 xml 加载篇
- Hibernate3源码分析之hibernate.cfg.xml配置文件与SessionFactory类
- Hibernate3源码分析之hibernate.cfg.xml配置文件与SessionFactory类
- hibernate加载hibernate.cfg.xml文件
- 【AMPPZ2014】【BZOJ4152】The Captain
- windows10任务管理器磁盘占用100%问题解决
- 宏、函数、宏函数的区别
- 程序设计原则
- error code 1592 Unsafe statement written to the binary log using statement...
- Hibernate_hibernate.cfg.xml加载流程源码分析
- 模板特化和偏特化
- ViewPager的切换动画
- RC充电计算
- Android教程 -08 ToolBar的使用和主题的介绍
- [leetcode] 52. N-Queens II
- nginx配置入门
- C++进行CGI的WEB编程
- Oracle数据库悲观锁与乐观锁详解