论坛首页 Java企业应用论坛

HibernateInterceptor到底起到甚么作用?

浏览 4206 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-12-30  
<bean id="classDao" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>limq.hibernate.dao.IClasses</value>
    </property>
    <property name="interceptorNames">
      <list>
        <value>hibernateInterceptor</value>
        <value>classDaoTarget</value>
      </list>
    </property>
  </bean>


这里好像是代理一个接口,但是为什么要制定拦截器呢?

<bean id="classDao" class="org.springframework.aop.framework.ProxyFactoryBean">
	    <property name="proxyInterfaces">
	      <value>limq.hibernate.dao.IClasses</value>
	    </property>
	    
	    <property name="target">
	    	<ref bean="classDaoTarget"/>
	    </property>
  	</bean>


这样代理一个接口不就可以了么? 为什么要写成上面的样子? 不解。
   发表时间:2006-12-30  
源码:
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
		boolean existingTransaction = false;
		Session session = SessionFactoryUtils.getSession(
				getSessionFactory(), getEntityInterceptor(), getJdbcExceptionTranslator());
		if (TransactionSynchronizationManager.hasResource(getSessionFactory())) {
			logger.debug("Found thread-bound Session for Hibernate interceptor");
			existingTransaction = true;
		}
		else {
			logger.debug("Using new Session for Hibernate interceptor");
			if (getFlushMode() == FLUSH_NEVER) {
				session.setFlushMode(FlushMode.NEVER);
			}
			TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));
		}
		try {
			enableFilters(session);
			Object retVal = methodInvocation.proceed();
			flushIfNecessary(session, existingTransaction);
			return retVal;
		}
		catch (HibernateException ex) {
			throw convertHibernateAccessException(ex);
		}
		finally {
			disableFilters(session);
			if (existingTransaction) {
				logger.debug("Not closing pre-bound Hibernate Session after interceptor");
			}
			else {
				TransactionSynchronizationManager.unbindResource(getSessionFactory());
				SessionFactoryUtils.releaseSession(session, getSessionFactory());
			}
		}
	}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics