fooky 2010-03-07 13:32 采纳率: 0%
浏览 222
已采纳

关于spring的注解式声明事务的问题

今天在写一个测试注解式事务的程序,不知道我的action一配置注解@Transactional,程序就运行不正常,请求各位帮忙看看是什么问题,部分代码如下:

1.接口:
package com.test.service;
public interface CifService {

public void save();

}
2.接口实现:

[color=red]//@Transactional(就是这个地方,我打开注释后就出错了,我想在save方法中使用事务)[/color]
public class CifServiceImpl implements CifService {
[color=red]……(省略无关代码) [/color] @Override
public void save() {

jdbcTemplate.update("update cif set introduction='11111' where cifname='test'");

// jdbcTemplate.update("update cif set introduction='2222' where cifname='aaaa");

}

}
3.JUnit测试类

public class TestCifService {

private static CifServiceImpl cifServiceImpl;

@BeforeClass
public static void setUpBeforeClass() throws Exception {

    try{
        ApplicationContext ctx = new ClassPathXmlApplicationContext("config/spring_config.xml");
        cifServiceImpl = (CifServiceImpl) ctx.getBean("cifServiceImpl");
    }catch(RuntimeException e){
        e.printStackTrace();
    }
}

@Test public void save(){
    cifServiceImpl.save();
}

}

4.spring配置文件:


5.出错信息:
java.lang.ClassCastException: $Proxy10 cannot be cast to com.test.service.impl.CifServiceImpl

各位帮忙看一下,谢谢!

  • 写回答

1条回答 默认 最新

  • wanghaolovezlq 2010-03-18 16:42
    关注

    cifServiceImpl = (CifServiceImpl) ctx.getBean("cifServiceImpl");
    是强制类型转换的错误啦

    应该这么写

    cifServiceImpl = (CifService) ctx.getBean("cifServiceImpl");

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序