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

来源:百度知道 编辑:UC知道 时间:2024/09/21 14:26:45
有一个项目的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 transac