no matching editors or conversion strategy found 异常

注意:JDK的动态代理,只能对实现接口的类实现代理,生成代理对象,如果这个类没有实现接口,是生成不了代理对象的。如本例UserManagerImpl实现了UserManager,如果去掉了UserManager接口,会出现异常(找不到cglib库)。

要解决这个问题,要添加cglib库,即:/spring_home/cglib/cglib-nodep-2.1_3.jar

 

如果目标对象实现了接口,默认情况下会使用JDK的动态代理实现AOP;如果有些目标对象没有实现接口,而有些又实现了,那么框架会在jdk动态代理和cglib直接灵活切换,实现了接口的使用动态代理,没有实现接口的采用cglib,因此较为保险的做法是总是引入cglib库。

如下面的代码, Spring注入的是接口,关联的是实现类。 这里注入了实现类,所以报异常了。 (出现在事务处理过程中)

 
  

id="aa"class="com.jdbcdemo.util.HibernateUtil"> id="ld"class="com.jdbcdemo.dao.impl.LogDaoImpl"> name="sessionFactory"ref="sessionFactory"> id="userDao"class="com.jdbcdemo.dao.impl.UserDaoImpl"> name="sessionFactory"ref="sessionFactory"> name="logDaoImpl"ref="ld">

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [E:\JavaProj\Hibernate_Spring_Study\WebRoot\WEB-INF\classes\applicationContext_beans.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy0 implementing com.jdbcdemo.dao.LogDao,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.jdbcdemo.dao.impl.LogDaoImpl] for property 'logDaoImpl'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0 implementing com.jdbcdemo.dao.LogDao,org.springframework.beans.factory.InitializingBean,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [com.jdbcdemo.dao.impl.LogDaoImpl] for property 'logDaoImpl':  no matching editors or conversion strategy found  
______________________________________________________________________________________________

解决办法1,  proxy-target-class="true"> ,表示可以用类注入:

 
  

xml version="1.0" encoding="UTF-8"?> xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"> name="sessionFactory"> bean="sessionFactory"/> transaction-manager="transactionManager"/> id="txAdvice"> name="add"/> proxy-target-class="true"> expression="execution(* com.jdbcdemo.dao.impl.*.*(..))"id="allManagerMethod"/> advice-ref="txAdvice"pointcut-ref="allManagerMethod"/>

解决办法2,在 tx:annotation-driven中表示可以用类注入
 
  

xml version="1.0" encoding="UTF-8"?>

 xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

 id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

 name="sessionFactory">

 bean="sessionFactory"/>

 transaction-manager="transactionManager"proxy-target-class="true"/>

 id="txAdvice"  >

 name="add"/>

 >

 expression="execution(* com.jdbcdemo.dao.impl.*.*(..))"id="allManagerMethod"/>

 advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>

你可能感兴趣的:(框架)