(转)Spring注入bean失败Failed to convert property value of type
Failed to convert property value of type [$Proxy13
Failed to convert property value of type [$Proxy13] to required type
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found
Caused by:
org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessException details (1) are:
PropertyAccessException 1:
org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found
Caused by:
java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:815)
当系统出现类似上面的异常信息时,很可能你引用了已经实现了自动代理的Bean, Cannot convert value of type [$Proxy13] to required type [com.makeprogress.dao.AuthorDaoImp] for property 'authorDaoImp' 提示的意思:你将authorDaoImp注入到一个spring受管Bean中, 但是spring容器提示你注入的Bean类型不对,它说你注入的是 [$Proxy13] 类型,这就说明你可能在无意中将authorDaoImp实现了代理, 此时你再在spring容器中引用 authorDaoImp时得到的将是代理类型。示例如下(在数据源,事务管理器配置完整的前提下):
<bean id="authorDaoImp" ref="sessionFactory"/>
</bean>
<!-- 配置事务拦截bean -->
<bean id="transactionInterceptor" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="save">PROPAGATION_REQUIRED</prop>
<prop key="Print*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<value>validateUserBean,authorDaoImp</value>
</property>
<property name="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时, 使用它的接口为其造型.