Results 1 to 5 of 5

Thread: Puzzling ClassCastException!

  1. #1
    Join Date
    Dec 2004
    Posts
    7

    Default Puzzling ClassCastException!

    Hi,

    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 1.5.0.8.

    Thanks for your help,



    Don

    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)

  2. #2
    Join Date
    Jan 2006
    Location
    Wellington, New Zealand
    Posts
    20

    Default

    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.

  3. #3
    Join Date
    Dec 2004
    Posts
    7

    Default Thanks.

    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.



    Don

  4. #4
    Join Date
    Jan 2006
    Location
    Wellington, New Zealand
    Posts
    20

    Default

    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.

  5. #5
    Join Date
    Jul 2006
    Location
    Philadelphia, PA, USA
    Posts
    341

    Default

    Quote Originally Posted by donb View Post
    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.
    Just to clarify given what I think is true:

    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.

    -Arthur Loder

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •