求教J2EE高手~!Spring配置文件applicationContext.xml的代码问题。

有一个项目的applicationContext.xml 文件中有如下这样一段代码,请问是用来做什么的?如下:
<bean id="hibernateInterceptor"
class="org.springframework.orm.hibernate3.HibernateInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>我自己做的添加了SSH的项目中的applicationContext.xml文件中没有这段代码。但是不知道那个项目中为什么要有这段代码。
请问,我不加这段代码也一样呀~! 加和不加这段代码有什么区别?你们回答的应该尽可能详细些。 谢谢。

This interceptor binds a new Hibernate Session to the thread before a method call, closing and removing it afterwards in case of any method outcome. If there already is a pre-bound Session (e.g. from HibernateTransactionManager, or from a surrounding Hibernate-intercepted method), the interceptor simply participates in it.
Application code must retrieve a Hibernate Session via the SessionFactoryUtils.getSession method or - preferably - Hibernate's own SessionFactory.getCurrentSession() method, to be able to detect a thread-bound Session. Typically, the code will look like as follows:

public void doSomeDataAccessAction() {
Session session = this.sessionFactory.getCurrentSession();
...
// No need to close the Session or translate exceptions!
}

The drawback is the dependency on interceptor configuration. However, note that this interceptor is usually not necessary in scenarios where the data access code always executes within transactions. A transaction will always have a thread-bound Session in the first place, so adding this interceptor to the configuration just adds value when fine-tuning Session settings like the flush mode - or when relying on exception translation.
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-10-07
自动帮你打开session
第2个回答  2009-10-07
打开session
第3个回答  2009-10-07
帮你打开(同时绑定在thread上)和关闭session,javadoc中写的很清楚。简单的说,用这个可以神不知鬼不觉的打开和关闭session,省去你在代码中的很多重复工作。
相似回答