package cn.com.oneslife;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.AfterClass;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.SessionHolder;
import org.springframework.transaction.support.TransactionSynchronizationManager;
public abstract class AbstractTest extends TestCase{
private static SessionFactory sessionFactory ;
private static ApplicationContext act;
public void setUp() throws Exception {
act = new ClassPathXmlApplicationContext("applicationContext.xml");
sessionFactory = (SessionFactory) getBean("sessionFactory");
Session s = sessionFactory.openSession();
TransactionSynchronizationManager.bindResource(sessionFactory,
new SessionHolder(s));
}
protected static Object getBean(String beanName) {
return act.getBean(beanName);
}
public void tearDown() throws Exception {
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager
.getResource(sessionFactory);
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSession(holder.getSession());
}
}
Spring+Hibernate延迟加载测试基类
最新推荐文章于 2024-11-02 18:17:01 发布