Hibernate源码1-基础案例

Hiberntae源码版本:

            5.0

入门代码示例:

// A SessionFactory is set up once for an application!
	final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
			.configure() // configures settings from hibernate.cfg.xml
			.build();
	try {
		sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
	}
	catch (Exception e) {
		// The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory
		// so destroy it manually.
		StandardServiceRegistryBuilder.destroy( registry );
	}

 这是官网入门的案例;主要是用一个标准服务注册机构建器来获得SessionFactory;

本文目的:

            1,了解StandardServiceRegistryBuilder

             2,探寻SessionFactory的由来;

源码阅读:

1,标准注册机构造器的构造方法


	/**
	 * Create a default builder.
	 * 创建一个默认的标准注册机的构造器
	 */
	public StandardServiceRegistryBuilder() {
		this( new BootstrapServiceRegistryBuilder().enableAutoClose().build() );
	}

	/**
	 * Create a builder with the specified bootstrap services.
	 * 创建一个带引导注册机的标准注册机的构造器
	 * @param bootstrapServiceRegistry Provided bootstrap registry to use.
	 */
	public StandardServiceRegistryBuilder(BootstrapServiceRegistry bootstrapServiceRegistry) {
		this( bootstrapServiceRegistry,  LoadedConfig.baseline() );
	}

	/**
	 * Create a builder with the specified bootstrap services.
	 * 创建一个引导注册机和配置信息的标准注册机的构造器 
	 * @param bootstrapServiceRegistry Provided bootstrap registry to use.
	 */
	public StandardServiceRegistryBuilder(BootstrapServiceRegistry bootstrapServiceRegistry, LoadedConfig loadedConfigBaseline) {
		this.settings = Environment.getProperties();
		this.bootstrapServiceRegistry = bootstrapServiceRegistry;
		this.configLoader = new ConfigLoader( bootstrapServiceRegistry );
		this.aggregatedCfgXml = loadedConfigBaseline;
	}

    标准注册机构造器有三个构造方法:分别为默认构造  引导其构造 和定制化构造;这三个构造器是联动的,我们可以默认初始化引导器注册机和默认路径配置信息;

    默认初始化引导器注册机:

this( new BootstrapServiceRegistryBuilder().enableAutoClose().build() );
/**
	 * See the discussion on {@link #disableAutoClose}.  This method enables
	 * the auto-closing.
	 *	设置注册机关闭方式为自动
	 * @return this, for method chaining
	 */
	public BootstrapServiceRegistryBuilder enableAutoClose() {
		this.autoCloseRegistry = true;
		return this;
	}
	
/**
	 * Build the bootstrap registry.
	 * 构建引导注册机
	 * @return The built bootstrap registry
	 */
	public BootstrapServiceRegistry build() {
		//类加载器服务    与JVM的类加载器不用 没有采用双亲委派机制(http://www.cnblogs.com/lanxuezaipiao/p/4138511.html)
		final ClassLoaderService classLoaderService;
		if ( providedClassLoaderService == null ) {
			// Use a set.  As an example, in JPA, OsgiClassLoader may be in both
			// the providedClassLoaders and the overridenClassLoader.
			//这段代码的意思没看明白  先不管应该是后面有用到
			final Set classLoaders = new HashSet();

			if ( providedClassLoaders != null )  {
				classLoaders.addAll( providedClassLoaders );
			}
			
			classLoaderService = new ClassLoaderServiceImpl( classLoaders );
		}else {
			classLoaderService = providedClassLoaderService;
		}

		final IntegratorServiceImpl integratorService = new IntegratorServiceImpl(
				providedIntegrators,
				classLoaderService
		);

		//开始实例化引导注册器   入参有注册器关闭策略   类加载器服务 策略选择器 ???
		return new BootstrapServiceRegistryImpl(
				autoCloseRegistry,
				classLoaderService,
				strategySelectorBuilder.buildSelector( classLoaderService ),
				integratorService
		);
	}

至此标注注册机构造器已经拥有了引导器注册机;联动至第三个构造方法  我们现在已经拥有了一个标准注册机的注册器

2,configure()方法 用来读取hbm.cfg.xml信息

3,build()构建标准注册机构造器

5,多元数据源MetadataSources

/**
 * Entry point into working with sources of metadata information (mapping XML, annotations).   Tell Hibernate
 * about sources and then call {@link #buildMetadata()}, or use {@link #getMetadataBuilder()} to customize
 * how sources are processed (naming strategies, etc).
 *  映射配置文件    注解的入口;   
 * @author Steve Ebersole
 *
 * @since 5.0
 */
public MetadataSources() {
		this( new BootstrapServiceRegistryBuilder().build() );
	}

	/**
	 * Create a metadata sources using the specified service registry.
	 *
	 * @param serviceRegistry The service registry to use.
	 */
	public MetadataSources(ServiceRegistry serviceRegistry) {
		// service registry really should be either BootstrapServiceRegistry or StandardServiceRegistry type...
		if ( ! isExpectedServiceRegistryType( serviceRegistry ) ) {
			LOG.debugf(
					"Unexpected ServiceRegistry type [%s] encountered during building of MetadataSources; may cause " +
							"problems later attempting to construct MetadataBuilder",
					serviceRegistry.getClass().getName()
			);
		}
		this.serviceRegistry = serviceRegistry;
		this.xmlMappingBinderAccess = new XmlMappingBinderAccess( serviceRegistry );
	}

 

 

 

 

    

        

转载于:https://my.oschina.net/ZGang/blog/675601

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值