cocoon-dev mailing list archives

ClassCastException becuase of different classloaders for a bean weaving by springframework

Date

Wed, 20 Sep 2006 10:59:37 GMT

Hi,
I have a bean created thru. springframeowrk in cocoon. The bean needs 3
arguments in its contructor to create it as below:
<bean id="platform" class="xs.school.Platform" scope="singleton">
<constructor-arg type="java.lang.String" value="development"/>
<constructor-arg type="xs.persistence.Adapter"
ref="persistenceAdapter"/>
<constructor-arg type="java.util.Locale"><util:constant
static-field="java.util.Locale.TAIWAN"/></constructor-arg>
</bean>
<bean id="persistenceAdapter" class="xs.selection.HibernateAdapter">
<property name="settings" value="hibernate.cfg.xml"/>
</bean>
I find xs.persistence.Adapter 's (which is an interface) classloader is
WebAppClassLoader. The real adapter implemenation is called HibernateAdapter
whose classloader is DefaultClassLoader. So a ClassCastException is thrown
when assigning the instance of HibernateAdapter (which implements Adapter,
surely) to the second argument shown beflow. Adapter and HibernateAdapter
are in two different jars but both are in WEB-INF/cocoon/lib. Should not all
classes in jars under WEB-INF/cocoon/lib loaded by the same classloader
(DefaultClassLoader)?
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Err
or creating bean with name 'platform' defined in URL
[file:C:/tmp/cocoon/xs-scho
ol-cocoon/src/main/resources/META-INF/spring/application.xml]: Unsatisfied
depen
dency expressed through constructor argument with index 1 of type [
xs.persistenc
e.Adapter]: Could not convert constructor argument value [
xs.selection.Hibernate
Adapter@779f5e] to required type [xs.persistence.Adapter]: Failed to convert
val
ue of type [xs.selection.HibernateAdapter] to required type [
xs.persistence.Adap
ter]; nested exception is java.lang.IllegalArgumentException: No matching
editor
s or conversion strategy found
at
org.springframework.beans.factory.support.ConstructorResolver.createA
rgumentArray(ConstructorResolver.java:375)
at
org.springframework.beans.factory.support.ConstructorResolver.autowir
eConstructor(ConstructorResolver.java:126)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:548)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBean
Factory.createBean(AbstractAutowireCapableBeanFactory.java:352)
at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getOb
ject(AbstractBeanFactory.java:240)
at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistr
y.getSingleton(DefaultSingletonBeanRegistry.java:132)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:237)
at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean
(AbstractBeanFactory.java:153)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.
preInstantiateSingletons(DefaultListableBeanFactory.java:254)
at
org.springframework.context.support.AbstractApplicationContext.refres
h(AbstractApplicationContext.java:337)
at
org.springframework.web.context.support.AbstractRefreshableWebApplica
tionContext.refresh(AbstractRefreshableWebApplicationContext.java:156)
at
org.springframework.web.context.ContextLoader.createWebApplicationCon
text(ContextLoader.java:246)
at
org.springframework.web.context.ContextLoader.initWebApplicationConte
xt(ContextLoader.java:184)
at
org.springframework.web.context.ContextLoaderListener.contextInitiali
zed(ContextLoaderListener.java:49)
at org.apache.cocoon.bootstrap.servlet.ShieldingListener.invoke
(Shieldin
gListener.java:151)
... 32 more
Rice