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??