Spring框架学习[HibernateTemplate对Hibernate的封装](五)
ctory buildSessionFactory() throws Exception { //获取Hibernate配置 Configuration config = newConfiguration(); //获取数据源 DataSource dataSource = getDataSource(); //配置数据源,事务管理器,缓存等等,将这些配置设置当对应的线程局部变量中, //使资源和当前线程绑定起来 if (dataSource != null) { //配置数据源 configTimeDataSourceHolder.set(dataSource); } if (this.jtaTransactionManager != null) { //配置jta事务管理器 configTimeTransactionManagerHolder.set(this.jtaTransactionManager); } if (this.cacheRegionFactory != null) { //配置缓存区域工厂 configTimeRegionFactoryHolder.set(this.cacheRegionFactory); } if (this.cacheProvider != null) { //配置缓存提供者 configTimeCacheProviderHolder.set(this.cacheProvider); } if (this.lobHandler != null) { //配置lob处理器,用于处理clob/blob等大字段类型映射 configTimeLobHandlerHolder.set(this.lobHandler); } //Hibernate不允许显示设置类加载器,因此需要暴露相应的线程上下文类加载器 Thread currentThread = Thread.currentThread(); ClassLoader threadContextClassLoader = currentThread.getContextClassLoader(); boolean overrideClassLoader = (this.beanClassLoader != null && !this.beanClassLoader.equals(threadContextClassLoader)); //用当前类加载器覆盖当前线程上下文类加载器 if (overrideClassLoader) { currentThread.setContextClassLoader(this.beanClassLoader); } //配置Hibernate相关属性 try { //如果Hibernate会话工厂暴露一个会话包装的代理 if (isExposeTransactionAwareSessionFactory()) { //使用Spring管理的Session作为Hibernate当前会话 config.setProperty( Environment.CURRENT_SESSION_CONTEXT_CLASS, SpringSessionContext.class.getName()); } //如果Hibernate会话工厂指定了事务管理器,则使用Hibernate的事务管理器 if (this.jtaTransactionManager != null) { config.setProperty( Environment.TRANSACTION_STRATEGY, JTATransactionFactory.class.getName()); config.setProperty( Environme
nt.TRANSACTION_MANAGER_STRATEGY, LocalTransactionManagerLookup.class.getName()); } //如果Hibernate会话工厂没有指定事务管理器,则使用Spring管理的事务 else { config.setProperty( Environment.TRANSACTION_STRATEGY, SpringTransactionFactory.class.getName()); } //设置SessionFactory级别的实体拦截器 if (this.entityInterceptor != null) { config.setInterceptor(this.entityInterceptor); } //设置命名规则 if (this.namingStrategy != null) { config.setNamingStrategy(this.namingStrategy); } //注册指定的Hibernate类型定义 if (this.typeDefinitions != null) { //通过JDK反射机制,获取Hibernate配置文件中的createMappings方法 Method createMappings = Configuration.class.getMethod("createMappings"); //通过JDK反射,获取createMappings方法返回的org.hibernate.cfg.Mappings对//象的addTypeDef方法,该方法的三个参数类型为:String,String和Properties Method addTypeDef = createMappings.getReturnType().getMethod( "addTypeDef", String.class, String.class, Properties.class); //通过JDK反射机制调用Hibernate配置中的createMappings方法, //返回org.hibernate.cfg.Mappings对象 Object mappings = ReflectionUtils.invokeMethod(createMappings, config); //遍历Hibernate中所有注册的类型定义 for (TypeDefinitionBean typeDef : this.typeDefinitions) { //通过反射机制,调用org.hibernate.cfg.Mappings对象的 //addTypeDef方法,该方法的三个参数分别为:类型名称,类型实 //现类,类型参数,为Hibernate添加类型定义 ReflectionUtils.invokeMethod(addTypeDef, mappings, typeDef.getTypeName(), typeDef.getTypeClass(), typeDef.getParameters()); } } //注册Hibernate过滤器 if (this.filterDefinitions != null) { for (FilterDefinition filterDef : this.filterDefinitions) { config.addFilterDefinition(filterDef); } } //从给定资源路径加载Hibernate配置 if (this.configLocations != null) { for (Resource resource : this.configLocations) { config.configure(resource.getU