正确做法:
1.基础Test类
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
@TransactionConfiguration(defaultRollback=false)
public class TestBase extends AbstractTransactionalJUnit4SpringContextTests{
@Override
@Resource(name="gameDataSource")
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
}
2.继承的类
@TransactionConfiguration(transactionManager="gameTransactionManager",defaultRollback=false)
public class MenuBoTest extends TestBase{
@Autowired
private MenuInfoBo menuInfoBo;
@Test
public void test(){
MenuInfo menuInfo=new MenuInfo();
menuInfo.setMenuName("本地游戏");
menuInfo.setMenuOpenType(1);
menuInfo.setMenuOrder(1);
menuInfoBo.add(menuInfo);
}
}
遇到的问题:
1.继承类MenuBoTest开始没有配注解@TransactionConfiguration
执行的时候会报错 显示No bean named 'transactionManager' is defined
源代码如下
private TransactionConfigurationAttributes retrieveTransactionConfigurationAttributes(Class<?> clazz) {
Class<TransactionConfiguration> annotationType = TransactionConfiguration.class;
TransactionConfiguration config = clazz.getAnnotation(annotationType);
if (logger.isDebugEnabled()) {
logger.debug("Retrieved @TransactionConfiguration [" + config + "] for test class [" + clazz + "]");
}
String transactionManagerName;
boolean defaultRollback;
if (config != null) {
transactionManagerName = config.transactionManager();
defaultRollback = config.defaultRollback();
}
else {
transactionManagerName = (String) AnnotationUtils.getDefaultValue(annotationType, "transactionManager");
defaultRollback = (Boolean) AnnotationUtils.getDefaultValue(annotationType, "defaultRollback");
}
TransactionConfigurationAttributes configAttributes =
new TransactionConfigurationAttributes(transactionManagerName, defaultRollback);
if (logger.isDebugEnabled()) {
logger.debug("Retrieved TransactionConfigurationAttributes [" + configAttributes + "] for class [" + clazz + "]");
}
return configAttributes;
}
执行的时候默认会加上事务,事务的bean是transactionManager,因为配置中没有这个bean所以会抛出这个异常。解决办法就是在继承类上加上注解@TransactionConfiguration(transactionManager="gameTransactionManager"),使用配置已经配过的这个gameTransactionManager。
2.经过上面的步骤测试又出现问题,可以查询但是不能进行增删改的操作,猜测原因可能是事务回滚造成的,所以在继承类加上defaultRollback=false,重新测试成功。
1.基础Test类
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
@TransactionConfiguration(defaultRollback=false)
public class TestBase extends AbstractTransactionalJUnit4SpringContextTests{
@Override
@Resource(name="gameDataSource")
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}
}
2.继承的类
@TransactionConfiguration(transactionManager="gameTransactionManager",defaultRollback=false)
public class MenuBoTest extends TestBase{
@Autowired
private MenuInfoBo menuInfoBo;
@Test
public void test(){
MenuInfo menuInfo=new MenuInfo();
menuInfo.setMenuName("本地游戏");
menuInfo.setMenuOpenType(1);
menuInfo.setMenuOrder(1);
menuInfoBo.add(menuInfo);
}
}
遇到的问题:
1.继承类MenuBoTest开始没有配注解@TransactionConfiguration
执行的时候会报错 显示No bean named 'transactionManager' is defined
源代码如下
private TransactionConfigurationAttributes retrieveTransactionConfigurationAttributes(Class<?> clazz) {
Class<TransactionConfiguration> annotationType = TransactionConfiguration.class;
TransactionConfiguration config = clazz.getAnnotation(annotationType);
if (logger.isDebugEnabled()) {
logger.debug("Retrieved @TransactionConfiguration [" + config + "] for test class [" + clazz + "]");
}
String transactionManagerName;
boolean defaultRollback;
if (config != null) {
transactionManagerName = config.transactionManager();
defaultRollback = config.defaultRollback();
}
else {
transactionManagerName = (String) AnnotationUtils.getDefaultValue(annotationType, "transactionManager");
defaultRollback = (Boolean) AnnotationUtils.getDefaultValue(annotationType, "defaultRollback");
}
TransactionConfigurationAttributes configAttributes =
new TransactionConfigurationAttributes(transactionManagerName, defaultRollback);
if (logger.isDebugEnabled()) {
logger.debug("Retrieved TransactionConfigurationAttributes [" + configAttributes + "] for class [" + clazz + "]");
}
return configAttributes;
}
执行的时候默认会加上事务,事务的bean是transactionManager,因为配置中没有这个bean所以会抛出这个异常。解决办法就是在继承类上加上注解@TransactionConfiguration(transactionManager="gameTransactionManager"),使用配置已经配过的这个gameTransactionManager。
2.经过上面的步骤测试又出现问题,可以查询但是不能进行增删改的操作,猜测原因可能是事务回滚造成的,所以在继承类加上defaultRollback=false,重新测试成功。