Configuration configuration = new Configuration().configure();
关于这一句的源码解读
1.调用configure()方法,configure()调用重载configure(String str)方法 资源文件 /hibernate.cfg.xml
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 );
}
2.getConfigurationInputStream(String str) 调用 ConfigHelper.getResourceAsStream(String str)方法 来获取一个输入流
public static InputStream getResourceAsStream(String resource) {
3.在重载configure(String str)中,调用doConfigure(Stream stream,String 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;
}
4. 1).根据资源名称获取日志
2).然后利用获得的inputStream流获取文档对象
3).如果日志有错误,抛异常
4).否则就调用重载方法doConfigure(doc) 读取文档文件
5.获取根元素sfNode,session-factory , 然后获取name
6.addProperties( sfNode );
parseSessionFactory( sfNode, name );
添加到Properties文件中,