Announcement Announcement Module
Collapse
No announcement yet.
ClassCastException: Error in WAR Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • ClassCastException: Error in WAR

    Hi, I am facing a problem while retrieving objects from a list obtained from another package.

    Code:
    Exception in thread "main" java.lang.ClassCastException: com.infosys.ingreen.middleware.entity.DeviceEntity cannot be cast to com.infosys.ingreen.middleware.entity.DeviceEntity
    This is the code of my com.infosys.ingreen.middleware.service.DeviceServi ce class:

    Code:
    public class DeviceService implements DeviceServiceInterface{   
           
        public List<DeviceEntity> getDevices()   
        {   
            DeviceDao deviceDao;   
            ApplicationContext context = new ClassPathXmlApplicationContext("middleware-dao.xml");   
            deviceDao = (DeviceDao) context.getBean("DeviceDao");   
            return deviceDao.getDevices();         
        }   
           
         public static void main(String args[])   
         {   
             DeviceService deviceService = new DeviceService();   
             List<DeviceEntity> deviceList = new ArrayList<DeviceEntity>();    
             deviceList = deviceService.getDevices();   
             int j=0;    
             while (j< deviceList.size())   
             {   
                    
                     System.out.println( deviceList.get(j).getId());   
                     j++;   
             }   
         }   
      }
    And this is middleware-dao.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>   
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
               xmlns:aop="http://www.springframework.org/schema/aop"  
                xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"  
                xmlns:tx="http://www.springframework.org/schema/tx"  
    xsi:schemaLocation="   
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd   
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">   
      
      
      
    <bean id="DeviceDao" class="com.infosys.ingreen.middleware.dao.DeviceDao">   
          <property name="entityManagerFactory" ref="entityManagerFactory"/>   
       </bean>   
      
    <bean    
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>   
     <context:annotation-config/>   
      
       <bean id="entityManagerFactory"    
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">    
    <property name="dataSource" ref="dataSource"/>    
    <property name="jpaVendorAdapter">    
    <bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter">     
    <property name="showSql" value="true"/>     
    <property name="generateDdl" value="true"/>     
    <property name="databasePlatform" value="oracle.toplink.essentials.platform.database.MySQL4Platform"/>   
     </bean>   
     </property>   
        
     <property name="loadTimeWeaver">   
     <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"/>   
     </property>   
     </bean>   
      
    </bean></beans>
    Now I know the problem is because of different class loaders. Now the thing is all the classes are in the same WAR, so same classloader should be used. What I assume is may be Spring is using its own class loader SimpleLoadTimeWeaver to load DeviceDao class. If that is the issue, then how can I resolve it? Otherwise what can be the problem??

  • #2
    Code:
    public class DeviceService implements DeviceServiceInterface{   
           
        public List<DeviceEntity> getDevices()   
        {   
            DeviceDao deviceDao;   
            ApplicationContext context = new ClassPathXmlApplicationContext("middleware-dao.xml");   
            deviceDao = (DeviceDao) context.getBean("DeviceDao");   
            return deviceDao.getDevices();         
        }   
           
         public static void main(String args[])   
         {   
             DeviceService deviceService = new DeviceService();   
             List<DeviceEntity> deviceList = new ArrayList<DeviceEntity>();    
             deviceList = deviceService.getDevices();   
             int j=0;    
             while (j< deviceList.size())   
             {   
                    
                     System.out.println( deviceList.get(j).getId());   
                     j++;   
             }   
         }   
      }
    NEVER NEVER NEVER (did I make myself clear) use code like that to obtain a reference to a bean... Unless you want to run out of memory, get performance problems, cripple your database etc. What you are doing is that you are creating a new instance of ALL OF YOUR BEANS each time you call getDevices()!!!

    Also are the 2 classes related (subclasses) if not the class cast exception is right that it cannot be cast...

    Comment


    • #3
      Alright..so then how would you get beans from context xml file? This is required, you need to get bean from xml. Thats how you will get the benefit of Spring.

      Comment


      • #4
        That is why we have something called dependency injection. You don't go around and create a new instance of your application context each time you need a bean. The context should be created once and your application wiring should be done by then. Your service class (although now also a bootstrap class) should have a reference to the dao (not the logic to construct a context, get bean) and simply call the method on there.

        Comment


        • #5
          I have tried what u said. I created the bean in main class and passed bean as parameter to the service class. But the same error is coming. First thing is that the service class is being called once only. So the bean is also being created once. Now even if i should not create bean in service class, where should I create it? Because in all tutorials or examples, I have been seing bean class being created and used in the same manner.

          Comment

          Working...
          X