Failed to convert property value of type [$Proxy13] to required type

本文探讨了Spring框架中常见的Bean类型转换异常问题,详细解释了[$Proxy13]类型与预期类型不符的原因,并提供了几种实用的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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" class="com.makeprogress.dao.AuthorDaoImp">
          <property name="hibernateTemplate">
                  <ref bean="hibernateTemplate"/>
          </property>
    </bean>
      

    <bean id="userLoginBo" class="com.makeprogress.bo.UserLoginBo">
           <property name="authorDaoImp">
                  <ref bean="authorDaoImp"/>
           </property>
         
    </bean>
    
         <!-- 配置事务管理器 -->
       <bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
          <property name="sessionFactory" ref="sessionFactory"/>
       </bean>
       
      
       <!-- 配置事务拦截bean -->
       <bean id="transactionInterceptor"  class="org.springframework.transaction.interceptor.TransactionInterceptor">


       <!-- 事务拦截bean需要注入一个事务管理器 -->
             <property name="transactionManager" 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时, 使用它的接口为其造型. 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值