Spring JPA same class gets loaded twice by different class loaders

最近在看《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>



首先在網上搜了下,說是同一個Class,被不同的Class Loader 加載,所以會出現 ClassCastException。
然後,具體的將各個Class的ClassLoader打印出來看了下,發現真的不一樣:

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



從結果可以看出,一個OrderDetail用的是sun提供的ClassLoader,另一個用的是Spring的,而Spring這個明顯是從Sun的ClassLoader中繼承過來的。
所以解決方法是重寫 SimpleLoadTimeWeaver:


public class MyLoadTimeWeaver extends SimpleLoadTimeWeaver {

	@Override
	public ClassLoader getInstrumentableClassLoader() {
		// TODO Auto-generated method stub
		return super.getInstrumentableClassLoader().getParent();
//		return super.getInstrumentableClassLoader();
	}
}


然後將配置文件中的LoadTimeWeaver屬性的值變成:
<property name="loadTimeWeaver">
			<bean class="spring.in.action.bean.util.MyLoadTimeWeaver"></bean>
</property>

這樣問題就解決了。


最後的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="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>



===========================================================================================================
那可不可以不用SimpleLoadTimeWeaver呢?因爲用了它,我們就會改變Class的ClassLoader,造成同一個Class會被不同的ClassLoader加載。試了下在EntityManagerFactory的bean中,去掉loadtimeWeaver屬性,得到的卻是另一個異常:
java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified

這個問題沒有去研究,希望有研究的朋友,可以分享下




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章