Spring框架学习[HibernateTemplate对Hibernate的封装](四)

2014-11-24 03:05:48 · 作者: · 浏览: 13
oC容器初始化回调方法 public void afterPropertiesSet() throws Exception {//委托模式,调用AbstractSessionFactoryBean子类//LocalSessionFactoryBean实现的方法 SessionFactory rawSf = buildSessionFactory(); //如果需要,将给定的会话工厂使用代理包装 this.sessionFactory = wrapSessionFactoryIfNecessary(rawSf); //钩子方法,Hibernate会话工厂创建成功后,应用自定义处理 afterSessionFactoryCreation(); } //如果有需要,将给定的Hibernate会话工厂用代理封装 protected SessionFactory wrapSessionFactoryIfNecessary(SessionFactory rawSf) { return rawSf; } //应用从Spring管理Hibernate会话工厂的IoC容器中获取SessionFactory protected final SessionFactory getSessionFactory() { if (this.sessionFactory == null) { throw new IllegalStateException("SessionFactory not initialized yet"); } return this.sessionFactory; } //当Spring管理Hibernate会话工厂的IoC容器关闭时关闭所创建的会话工厂 public void destroy() throws HibernateException { logger.info("Closing Hibernate SessionFactory"); try { beforeSessionFactoryDestruction(); } finally { this.sessionFactory.close(); } }//获取单态模式的Hibernate会话工厂 public SessionFactory getObject() { return this.sessionFactory; } //获取Hibernate会话工厂的对象类型 public Class< extends SessionFactory> getObjectType() { return (this.sessionFactory != null this.sessionFactory.getClass() : SessionFactory.class); } public boolean isSingleton() { return true; }//转换异常 public DataAccessException translateExceptionIfPossible(RuntimeException ex) { //如果异常时Hibernate异常类型 if (ex instanceof HibernateException) { //转换Hibernate异常 return convertHibernateAccessException((HibernateException) ex); } //如果不是Hibernate异常,则返回null return null; }//转换Hibernate异常 protected DataAccessException convertHibernateAccessException(HibernateException ex) { //如果Hibernate会话工厂设置了Jdbc异常转换器,且给定的异常是jdbc异常类型, //则使用设置的jdbc异常转换器转换给定的Hibernate异常 if (this.jdbcExceptionTranslator != null && ex instanceof JDBCException) { JDBCException jdbcEx = (JDBCException) ex; return this.jdbcExceptionTranslator.translate( "Hibernate operation: " + jdbcEx.getMessage(), jdbcEx.getSQL(), jdbcEx.getSQLException()); }//如果Hibernate会话工厂没有设置jdbc异常转换器,或者给定异常不是jdbc异常//类型,则使用Hibernate默认的异常转换器转换 return SessionFactoryUtils.convertHibernateAccessException(ex); }//创建Hibernate会话工厂 protected abstract SessionFactory buildSessionFactory() throws Exception; //钩子方法,Hibernate会话工厂成功创建后操作处理 protected void afterSessionFactoryCreation() throws Exception { } //钩子方法,Hibernate会话工厂关闭前操作处理 protected void beforeSessionFactoryDestruction() { }}

通过分析Spring对Hibernate会话工厂管理的IoC容器AbstractSessionFactoryBean的源码,我们可以了解到,AbstractSessionFactoryBean继承了InitializingBean接口,并实现了其afterPropertiesSet方法,该方法是在Spring IoC容器初始化完成之后由IoC容器回调的方法,分析AbstractSessionFactoryBean的工作流程如下:

(1).Spring管理Hibernate会话工厂的IoC容器AbstractSessionFactoryBean初始化完成,IoC容器回调afterPropertiesSet方法创建单态模式的Hivbernate会话工厂。

(2).应用通过getObject方法向Spring管理Hibernate会话工厂的IoC容器AbstractSessionFactoryBean索取Hibernate会话工厂。

下面我们继续分析AbstractSessionFactoryBean子类LocalSessionFactoryBean创建Hibernate会话工厂的过程。

3.LocalSessionFactoryBean创建SessionFactory:

Spring中管理Hibernate会话工厂的IoC容器AbstractSessionFactoryBean通过委派模式调用其子类LocalSessionFactoryBean的buildSessionFactory创建Hibernate会话工厂,源码如下:

[java] view plaincopyprint //创建Hibernate会话工厂 protected SessionFa