Hibernate源码1-基础案例
Hibernate源码1-基础案例
天呀鲁哇 发表于2年前
Hibernate源码1-基础案例
  • 发表于 2年前
  • 阅读 49
  • 收藏 1
  • 点赞 2
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

摘要: StandardServiceRegistryBuilder

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 );
	}

 

 

 

 

    

        

共有 人打赏支持
粉丝 9
博文 82
码字总数 42007
评论 (0)
×
天呀鲁哇
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: