这个问题类似于先前的one。我正在尝试在我的Spring-JUnit-Transactional测试之一中对Hibernate session 进行@Autowire
编码,但是却遇到了以下异常:java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional ...
这是我的JUnit类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager")
@Transactional
public class MyTest {
@Qualifier("session")
@Autowired
private Session session;
@Test
public void testSomething() {
session.get(User.class, "[email protected]");
}
}
如果我对
@Autowire
进行SessionFactory
并以编程方式(而不是在Spring XML中定义它)来获取Session
,则每个方法都可以正常运行,如下所示:@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml"})
@TransactionConfiguration(transactionManager="transactionManager")
@Transactional
public class MyTest{
@Qualifier("sessionFactory")
@Autowired
private SessionFactory sessionFactory;
@Test
public void testSomething() {
Session session = SessionFactoryUtils.getSession(sessionFactory, false);
session.get(User.class, "[email protected]");
}
}
但是,如果我使用
Session
在Spring XML中定义<aop:scoped-proxy />
,则可以使我的原始示例正常工作:<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
...
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"><value>classpath:/hibernate.cfg.xml</value></property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="session" class="org.springframework.orm.hibernate3.SessionFactoryUtils" factory-method="getSession" scope="prototype">
<constructor-arg ref="sessionFactory" />
<constructor-arg value="false" />
<!-- This is seems to be needed to get rid of the 'No Hibernate Session' error' -->
<aop:scoped-proxy />
</bean>
</beans>
我的问题是:鉴于单元测试中应该只有一个线程绑定(bind)的事务上下文,为什么需要
<aop:scoped-proxy />
? 是什么定义我的Hibernate Session
bean的正确方法是? 最佳答案
SessionFactoryUtils.getSession()与获取Session的任何其他方式一样好。它所做的与HibernateDaoSupport.getSession()相同。
您需要使用scoped-proxy的原因是由于时间问题。没有scoped-proxy,似乎它是在测试开始之前,因此在事务开始之前注入(inject) session ,因此您会得到错误。
通过添加scoped-proxy,它可以代理Session并进行注入(inject),因此它不会预先注入(inject)实际的 session (在事务开始之前),而是仅在测试运行时(实际上需要进行测试时)获取并进行调用。反对它。