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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器