最近在看《Spring In Action》,在做测试时,碰到了一个问题,那就是:
同一个Class,却被不同的Class Loader 加载,出现的异常为:
java.lang.ClassCastException: spring.in.action.bean.chapter05.OrderDetail cannot be cast to spring.in.action.bean.chapter05.OrderDetail.
Spring的配置文件内容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- <value>classpath:/*/*/datasource.properties</value> --> <value>F:\Eclipse2\spring-in-action\config\datasource.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"> <property name="driverClassName" value="${datasource.driverClassName}"></property> <property name="url" value="${datasource.url}"></property> <property name="username" value="${datasource.username}"></property> <property name="password" value="${datasource.password}"></property> <property name="maxActive" value="5"></property> <property name="minIdle" value="3"></property> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <property name="database" value="MYSQL"></property> <property name="showSql" value="true"></property> <property name="generateDdl" value="true"></property> </bean> </property> <property name="jpaProperties"> <props> <prop key="eclipselink.ddl-generation">NONE</prop> </props> </property> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.SimpleLoadTimeWeaver"></bean> </property> </bean> <bean id="orderDetailDao" class="spring.in.action.bean.chapter05.OrderDetailDaoImpl"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> </beans>
List<OrderDetail> orderDetailList = getJpaTemplate().find("select orderDetail from OrderDetail orderDetail"); if(!orderDetailList.isEmpty()){ for(Object obj : orderDetailList){ System.out.println("Element:"+obj.getClass().getClassLoader()); System.out.println("order:"+OrderDetail.class.getClassLoader()); break; } }else{ System.out.println("orderDetail list is null"); }
Element: org.springframework.instrument.classloading.SimpleInstrumentableClassLoader@15c07d8 order: sun.misc.Launcher$AppClassLoader@19821f
public class MyLoadTimeWeaver extends SimpleLoadTimeWeaver { @Override public ClassLoader getInstrumentableClassLoader() { // TODO Auto-generated method stub return super.getInstrumentableClassLoader().getParent(); // return super.getInstrumentableClassLoader(); } }
<property name="loadTimeWeaver"> <bean class="spring.in.action.bean.util.MyLoadTimeWeaver"></bean> </property>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <!-- <value>classpath:/*/*/datasource.properties</value> --> <value>F:\Eclipse2\spring-in-action\config\datasource.properties</value> </list> </property> </bean> <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"> <property name="driverClassName" value="${datasource.driverClassName}"></property> <property name="url" value="${datasource.url}"></property> <property name="username" value="${datasource.username}"></property> <property name="password" value="${datasource.password}"></property> <property name="maxActive" value="5"></property> <property name="minIdle" value="3"></property> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <property name="database" value="MYSQL"></property> <property name="showSql" value="true"></property> <property name="generateDdl" value="true"></property> </bean> </property> <property name="jpaProperties"> <props> <prop key="eclipselink.ddl-generation">NONE</prop> </props> </property> <property name="loadTimeWeaver"> <bean class="spring.in.action.bean.util.MyLoadTimeWeaver"></bean> </property> </bean> <bean id="orderDetailDao" class="spring.in.action.bean.chapter05.OrderDetailDaoImpl"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> </beans>
java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified