Results 1 to 4 of 4

Thread: Multiple factories causing this?

  1. #1
    Join Date
    Mar 2005
    Posts
    25

    Default Multiple factories causing this?

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

    Code:
    Resource    res = new FileSystemResource("flightAvailabilityTest.xml");
    BeanFactory factory = new XmlBeanFactory(res);
     
    //Get the requests container
    FlightAvailabilityRequestsHolder requestsHolder = null;
    
    try
    {
             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. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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.

    Regards,
    Andreas

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Are the classes loaded in the same classloader? If not then you will also get a ClassCastException.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4
    Join Date
    Mar 2005
    Posts
    25

    Default

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

Posting Permissions

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