Announcement Announcement Module
No announcement yet.
Multiple factories causing this? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple factories causing this?

    I'm getting a ClassCastException when running the following code:

    Resource    res = new FileSystemResource("flightAvailabilityTest.xml");
    BeanFactory factory = new XmlBeanFactory(res);
    //Get the requests container
    FlightAvailabilityRequestsHolder requestsHolder = null;
             requestsHolder = (FlightAvailabilityRequestsHolder) factory.getBean("flightAvailability");
    catch(ClassCastException cce)
             LOG.error("ClassCastException casting to FlightAvailabilityRequestsHolder.\n" +
                   "Class found was:    " + factory.getBean("flightAvailability").getClass().getName() + "\n" +
                  "Class expected was: " + FlightAvailabilityRequestsHolder.class.getName());
    14:33:18,508 XmlBeanFactory DEBUG - Returning cached instance of singleton bean 'flightAvailability'
    14:33:18,509 FlightAvailabilityTestCase ERROR - ClassCastException casting to FlightAvailabilityRequestsHolder.
    Class found was: com.mycorp.test.FlightAvailabilityRequestsHolder
    Class expected was: com.mycorp.test.FlightAvailabilityRequestsHolder

    Notice that these are the same!

    If I run this as a stand alone application, all works just fine, but when I put it inside another Spring based app (a framework that I'm trying to incorporate into) I get this problem.

    Is there some issue with having multiple BeanFactory classes maybe?

    Note that I don't have easy access to th current BeanFactory and it is set up to load a specific application context file and I need to add my own file apart from it, and that is why I went this route.

    Any help is greatly appreciated!

  • #2
    This sounds like a classloading issue. The same class seems to be loaded by different classloaders, resulting in the exception you see.

    As for accessing the BeanFactory from multiple locations: Maybe you could access the BeanFactory where it is possible and store it in a well known place (e.g. a static variable if possible). From there you might access it later.



    • #3
      Are the classes loaded in the same classloader? If not then you will also get a ClassCastException.


      • #4
        I'm fairly sure that the framework has a custom class class loader.