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.
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.
来自:
学习spring 依赖注入感受:
依赖的是接口,不依赖实现。
$Proxy1 cannot be cast to com.inspur.democreen.dao.impl.TestDaoImpl
定义一个接口对象,而不是定义 实现接口类的对象,依赖的是接口,不依赖实现,依赖的是描述。
本文探讨了Spring框架中出现的依赖注入异常问题,具体表现为ClassCastException异常,详细分析了问题的原因在于试图将代理对象强制转换为实现类。文章提供了解决方案,建议依赖于接口而非实现,并展示了正确的依赖注入方式。
1万+

被折叠的 条评论
为什么被折叠?



