配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="entityFactory"
class="com.util.EntityFactory">
</bean>
<bean id="DataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@172.16.21.243:1521:g5db</value>
</property>
<property name="username">
<value>dev</value>
</property>
<property name="password">
<value>dev</value>
</property>
</bean>
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="DataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingResources">
<list><value>com/dao/cfg/Users.hbm.xml</value></list>
</property>
</bean>
<!-- DAO定义 -->
<bean id="userDao" class="com.dao.impl.UserDaoImpl">
<property name="sessionFactory">
<ref bean="SessionFactory" />
</property>
</bean>
<!-- Business定义 --> <bean id="userManagerBusiness"
class="com.business.impl.UserManagerBusiness">
<property name="userDao">
<ref local="userDao" />
</property>
</bean>
<!--事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="SessionFactory" />
</property>
</bean>
<bean id="baseTransactionManager" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="query*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="login">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="userServer" parent="baseTransactionManager">
<property name="target">
<ref local="userManagerBusiness" />
</property>
</bean>
</beans>代码:
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.business.IUserManagerBusiness;
import com.entity.IUser;
import com.util.BusinessException;
import com.util.EntityFactory;
import junit.framework.TestCase;
public class UserManagerBusinessTest extends TestCase {
private EntityFactory entityFactory;
private IUserManagerBusiness userManagerBusiness;
private IUser user;
private static ClassPathXmlApplicationContext context;
static {context = new ClassPathXmlApplicationContext("applicationContext.xml"); }
protected void setUp() throws Exception {
this.entityFactory = (EntityFactory) context.getBean("entityFactory");
this.userManagerBusiness = (IUserManagerBusiness) context.getBean("userManagerBusiness");
this.user = this.entityFactory.createUsers();
}
 public void testDeleteById() {
try {
this.userManagerBusiness.deleteById(23);
} catch (BusinessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
异常说session关闭,是不是我的事务没有配好,请帮忙看看错在哪里?是不是这样子配滴呀,命苦呀!!

解决方案 »

  1.   

    你的配置有问题。首先,你的那个getBean("userManagerBusiness")取的还是原始的bean,并没有取得代理的bean啊,应该getBean("userServer"),才能取得代理的bean,才能有事务管理的功能啊。其次,你get原始的那个业务bean的时候就出了异常,可能你的配置还是有问题的,如果没有问题,也是可以使用的,只是没有事务管理功能而已。你在根据具体控制台打印出来的异常信息查查看,你也没法异常的信息是什么,我们怎么帮助你啊~~下次再提问的时候,最好把关键的异常信息也一并的贴出来!~