(转)Spring注入bean失败Failed to convert property value of type

ScarletLina 2011-04-09

Failedtoconvertpropertyvalueoftype[$Proxy13

Failedtoconvertpropertyvalueoftype[$Proxy13]torequiredtype

PropertyAccessException1:org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy13]torequiredtype[com.makeprogress.dao.AuthorDaoImp]forproperty'authorDaoImp';nestedexceptionisjava.lang.IllegalArgumentException:Cannotconvertvalueoftype[$Proxy13]torequiredtype[com.makeprogress.dao.AuthorDaoImp]forproperty'authorDaoImp':nomatchingeditorsorconversionstrategyfound

Causedby:

org.springframework.beans.PropertyBatchUpdateException;nestedPropertyAccessExceptiondetails(1)are:

PropertyAccessException1:

org.springframework.beans.TypeMismatchException:Failedtoconvertpropertyvalueoftype[$Proxy13]torequiredtype[com.makeprogress.dao.AuthorDaoImp]forproperty'authorDaoImp';nestedexceptionisjava.lang.IllegalArgumentException:Cannotconvertvalueoftype[$Proxy13]torequiredtype[com.makeprogress.dao.AuthorDaoImp]forproperty'authorDaoImp':nomatchingeditorsorconversionstrategyfound

Causedby:

java.lang.IllegalArgumentException:Cannotconvertvalueoftype[$Proxy13]torequiredtype[com.makeprogress.dao.AuthorDaoImp]forproperty'authorDaoImp':nomatchingeditorsorconversionstrategyfound

atorg.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)

atorg.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)

atorg.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:815)

当系统出现类似上面的异常信息时,很可能你引用了已经实现了自动代理的Bean,Cannotconvertvalueoftype[$Proxy13]torequiredtype[com.makeprogress.dao.AuthorDaoImp]forproperty'authorDaoImp'提示的意思:你将authorDaoImp注入到一个spring受管Bean中,但是spring容器提示你注入的Bean类型不对,它说你注入的是[$Proxy13]类型,这就说明你可能在无意中将authorDaoImp实现了代理,此时你再在spring容器中引用authorDaoImp时得到的将是代理类型。示例如下(在数据源,事务管理器配置完整的前提下):

<beanid="authorDaoImp"class="com.makeprogress.dao.AuthorDaoImp">

<propertyname="hibernateTemplate">

<refbean="hibernateTemplate"/>

</property>

</bean>

<beanid="userLoginBo"class="com.makeprogress.bo.UserLoginBo">

<propertyname="authorDaoImp">

<refbean="authorDaoImp"/>

</property>

</bean>

<!--配置事务管理器-->

<beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<propertyname="sessionFactory"ref="sessionFactory"/>

</bean>

<!--配置事务拦截bean-->

<beanid="transactionInterceptor"class="org.springframework.transaction.interceptor.TransactionInterceptor">

<!--事务拦截bean需要注入一个事务管理器-->

<propertyname="transactionManager"ref="transactionManager"/>

<propertyname="transactionAttributes">

<props>

<propkey="insert*">PROPAGATION_REQUIRED</prop>

<propkey="find*">PROPAGATION_REQUIRED</prop>

<propkey="add*">PROPAGATION_REQUIRED</prop>

<propkey="save">PROPAGATION_REQUIRED</prop>

<propkey="Print*">PROPAGATION_REQUIRED</prop>

</props>

</property>

</bean>

<beanclass="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<propertyname="beanNames">

<value>validateUserBean,authorDaoImp</value>

</property>

<propertyname="interceptorNames">

<list>

<value>transactionInterceptor</value>

<!--

此处增加新的Interceptor

-->

</list>

</property>

</bean>

上面红色显示的authorDaoImp对象注入给了userLoginBo受管Bean,但是下面的配置中spring为authorDaoImp生成了自动代理,所以在上面的注入中注入的将是代理对象(提示信息中的[$Proxy13])而不是原来的authorDaoImp对象,这就产生了上面错误信息。

这种异常根据其产生的原因可以用下面二种方法解决:

如果异常产生原因是你将代理Bean注入给了其它受管Bean

1.将自动生成代理的authorDaoImp的配置去掉.

2.在接受注入的Bean中,将注入Bean类型改为它的接口类型.

3.不要将自动生成了代理的Bean注入给其它受管Bean.

如果异常产生原因是使用getBean("")方法产生时

2.不获得自动生成了代理的Bean.

3.在使用XXX.getBean("")方法得到生成了自动代理的受管Bean时,使用它的接口为其造型.

相关推荐

kyle00 / 0评论 2020-05-07