`

(转)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" 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时, 使用它的接口为其造型.
分享到:
评论

相关推荐

    Springmvc : Failed to convert property value of type 'java.lang.String' to int

    标题“Springmvc : Failed to convert property value of type 'java.lang.String' to int”涉及的是一个在使用Spring MVC框架时常见的错误。这个错误通常出现在尝试将一个字符串类型(String)的属性值转换为整型...

    spring boot 枚举使用的坑整理

    Spring Boot 枚举使用的坑整理 Spring Boot 枚举使用的坑整理是指在使用 Spring Boot 枚举时可能出现的一些问题和解决方法的总结。枚举类型是一种特殊的数据类型,它具有固定的值,且这些值不会被修改。在 Java 中...

    Spring定义bean的三种方式和自动注入

    在Spring框架中,管理Bean的方式主要有三种:XML配置、注解配置和Java配置。下面将详细介绍这三种方式以及Spring的自动注入机制。 1. **基于XML的Bean定义**: 在XML配置中,我们通常在`applicationContext.xml`...

    spring依赖注入bean

    **Spring 依赖注入 (DI) 与 Bean** Spring 框架的核心特性之一就是依赖注入(Dependency Injection,简称 DI),这是一种设计模式,它允许我们控制组件之间的耦合,而不是让组件自行创建它们所依赖的对象。这有助于...

    关于spring boot中几种注入方法的一些个人看法

    在使用 @Autowired 之前,我们对一个 bean 配置起属性时,是使用 &lt;property name="属性名" value=" 属性值"/&gt; 的方式来配置,比较繁琐,而且代码比较多。在 Spring 2.5 引入了 @Autowired 注释,我们平常直接引用的...

    Spring (bean怎样注入值)学习实例

    在Spring框架中,Bean的注入是其核心特性之一,它允许开发者通过声明式的方式管理对象的依赖关系。本文将深入探讨如何在Spring中通过XML配置文件对Bean进行值的注入,包括List、Set和Map等集合类型的注入。 首先,...

    spring bean XML配置入门

    在本文中,我们将深入探讨Spring框架中的Bean XML配置,这是Spring的核心特性之一,它允许我们定义、管理和装配应用中的对象。我们将围绕以下知识点展开: 1. **Spring框架基础**: Spring是一个开源的Java平台,...

    通过@Autowired注解注入bean的顺序,以及@bean注入.rar

    这个bean将被添加到Spring的bean定义中,并且可以在其他bean中通过`@Autowired`进行注入。`@Bean`可以包含额外的配置,如`@Lazy`(表示延迟初始化)、`@Profile`(环境特定的bean)等。 在`@Autowired`注入时的顺序...

    Spring的Bean配置

    Spring框架是Java开发中不可或缺的一部分,它以其控制反转(IoC)和依赖注入(DI)的核心特性,极大地简化了应用程序的构建与管理。在本文中,我们将深入探讨Spring中的Bean配置,包括IoC和DI的概念,Bean的配置方式...

    SPRING:bean配置properties

    这里,`propertyConfigurerForAnalysis`是`PropertyPlaceholderConfigurer`的实例bean,其作用是将`dbQuery.properties`中的键值对作为环境变量注入到Spring容器中。`classpath:`前缀表示该文件位于类路径下,即项目...

    day38 12-Spring的Bean的属性的注入:名称空间p

    在Spring框架中,Bean的属性注入是核心功能之一,它允许我们通过配置文件或注解来设置Bean实例的属性值,使得对象的初始化更加灵活。本文将深入探讨Spring中的"名称空间p",这是一种用于配置Bean属性注入的方式,...

    spring bean的生命周期

    - **XML配置**:在传统的Spring应用中,Bean的定义通常写在XML配置文件中,如`springbean-xml`中的配置。 - **注解配置**:使用`@Component`,`@Service`,`@Repository`和`@Controller`注解标记类,配合`@...

    Spring-注入依赖,AOP,自动注入Bean

    在Spring框架中,依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented Programming,AOP)是两大核心特性,同时自动注入Bean也是Spring管理对象的一种常见方式。让我们深入探讨这些概念。 首先,...

    day38 11-Spring的Bean的属性的注入:对象属性

    在Spring框架中,Bean的属性注入是核心功能之一,它允许我们为Bean对象设置各种属性值,以便在应用运行时创建和配置完全初始化的对象。在本篇内容中,我们将深入探讨Spring如何处理Bean的属性注入,包括对象属性的...

    day38 17-Spring的Bean的属性注入:注解方式

    在Spring框架中,Bean的属性注入是核心功能之一,它允许开发者在不编写代码的情况下配置对象的依赖关系。本文将详细讲解使用注解方式进行Bean属性注入的方法,以及相关的源码和工具应用。 首先,让我们了解Spring中...

    Spring bean 管理

    - setter方法注入:通过&lt;property&gt;元素设置的属性,Spring调用相应的setter方法来注入值。 6. Spring的属性注入-注解方式: 随着Spring的发展,注解方式的依赖注入变得越来越流行。通过在类属性上使用@Autowired、...

    day38 13-Spring的Bean的属性的注入:SpEL注入

    在Spring框架中,Bean的属性注入是其核心功能之一,使得我们可以轻松地管理对象的依赖关系,无需在代码中硬编码这些依赖。本篇将详细探讨Spring中的SpEL(Spring Expression Language)注入,这是一种强大的表达式...

    spring框架进行简单的增加操作出现的错误,解决方法

    nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Driver' for property 'driver';...

Global site tag (gtag.js) - Google Analytics