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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试