Oct 13th, 2006, 06:23 PM
I have a classcast exception if I cast the object from getBean("studentDAO") to the impl class which is com.mycom.integration.dao.ejb3.StudentDAO. But I don't get the exception if I cast it to the interface com.mycom.integration.dao.ejb3.IUserDAO. I would appreciate if anyone can shed somelight what is going on. And how would I be able to cast it to the impl class? I am using the latest Spring 2.0 and JDK 188.8.131.52.
Thanks for your help,
Bean named 'studentDAO' must be of type [com.mycom.integration.dao.ejb3.StudentDAO], but was actually of type [$Proxy27]
org.springframework.beans.factory.BeanNotOfRequire dTypeException: Bean named 'studentDAO' must be of type [com.mycom.integration.dao.ejb3.StudentDAO], but was actually of type [$Proxy27]
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:305)
info not weaving 'org/apache/tools/ant/util/DOMElementWriter'
at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:160)
at org.springframework.context.support.AbstractApplic ationContext.getBean(AbstractApplicationContext.ja va:646)
at com.mycom.integration.dao.ejb3.StudentDAOTest.setU p(StudentDAOTest.java:40)
Oct 13th, 2006, 10:06 PM
You can't; the bean is no longer of type StudentDAO as it's been replaced by a proxy class (hence the ClassCastException).
What I would suggest is that you provide an interface for each concrete class and use these when retrieving your beans (ie, writing a IStudentDAO class and casting to that).
You'll note a similar thing if you're using dependency injection; the accessors must reference the interface, not the concrete class.
Oct 13th, 2006, 10:19 PM
I remember now. I think if it is the interface then JDK reflection is used and if it is a concrete class CGLIB is used. That is why I can get the concete class if I just get rid of the interface, I think. Thanks for your help.
Oct 14th, 2006, 06:41 AM
Do you have good reasons for wanting to avoid interfaces and therefore cglib proxying? Using interfaces does tend to make things a lot more flexible in the long run if you're developing a reasonably-sized project.
Oct 16th, 2006, 10:03 AM
Just to clarify given what I think is true:
Originally Posted by donb
If you explicitly set the proxy interfaces (such as via the ProxyFactoryBean.setProxyInterfaces method), then JDK Proxies will be used. In this case, the proxy class will be a subclass of java.lang.reflect.Proxy, but the generated proxy class will implement your interfaces. In this case, you cannot cast to the implementation class because the proxy class is not a subclass of the implementation class.
If you do not set the proxy interfaces (and also do not set auto-detection of interfaces to true via the ProxyFactoryBean.setAutodetectInterfaces method), then CGLIB proxying will be performed. In this case, the generated proxy will be a subclass of your implementation class, and you can cast the proxy to your implementation class.
Lastly, if you need to use CGLIB proxying, your implementation class can still implement one or more interfaces; just don't set the proxyInterfaces. It's not the implementing of interfaces that matters, but whether you specify the interfaces in the proxy creation.