$Proxy1 cannot be cast

toHibernate+spring整合出现异常

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.ClassCastException: $Proxy1 cannot be cast to com.inspur.democreen.dao.impl.TestDaoImpl
        at com.inspur.democreen.test.Test.main(Test.java:16)

 

 

类型错误

Hi.
I was having a similar problem a few days a go.
I have a class A that implements an interface B. A is managed by spring (a DAO as a matter of fact), and whenever I try to inject an A instance into an A typed field, I get the exact same error.
The workaround I've found was to inject an A instance into a B typed field i.e to inject the instrumented instance (proxy to A) into an interface typed field.

Also, his class already implements an interface (so Spring uses the Java dynamic proxies).

Regards.
来自:
http://forum.springsource.org/archive/index.php/t-46403.html
学习spring 依赖注入感受:
依赖的是接口,不依赖实现。
$Proxy1 cannot be cast to com.inspur.democreen.dao.impl.TestDaoImpl
定义一个接口对象,而不是定义 实现接口类的对象,依赖的是接口,不依赖实现,依赖的是描述。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值