$Proxy1 cannot be cast

本文探讨了Spring框架中出现的依赖注入异常问题,具体表现为ClassCastException异常,详细分析了问题的原因在于试图将代理对象强制转换为实现类。文章提供了解决方案,建议依赖于接口而非实现,并展示了正确的依赖注入方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
定义一个接口对象,而不是定义 实现接口类的对象,依赖的是接口,不依赖实现,依赖的是描述。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值