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注入的是接口,关联的是实现类。 这里注入了实现类,所以报异常了。 (出现在事务处理过程中)

<beanid="aa"class="com.jdbcdemo.util.HibernateUtil"></bean> <beanid="ld"class="com.jdbcdemo.dao.impl.LogDaoImpl"> <propertyname="sessionFactory"ref="sessionFactory"></property> </bean> <beanid="userDao"class="com.jdbcdemo.dao.impl.UserDaoImpl"> <propertyname="sessionFactory"ref="sessionFactory"></property> <propertyname="logDaoImpl"ref="ld"></property> </bean>

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, <aop:config proxy-target-class="true"> ,表示可以用类注入:

<?xml version="1.0" encoding="UTF-8"?> <beansxmlns="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"> <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <propertyname="sessionFactory"> <refbean="sessionFactory"/> </property> </bean> <tx:annotation-driventransaction-manager="transactionManager"/> <tx:adviceid="txAdvice"> <tx:attributes> <tx:methodname="add"/> </tx:attributes> </tx:advice> <aop:configproxy-target-class="true"> <aop:pointcutexpression="execution(* com.jdbcdemo.dao.impl.*.*(..))"id="allManagerMethod"/> <aop:advisoradvice-ref="txAdvice"pointcut-ref="allManagerMethod"/> </aop:config> </beans>

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

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

<beans 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">

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

<property name="sessionFactory">

<ref bean="sessionFactory"/>

</property>

</bean>

<tx:annotation-driven transaction-manager="transactionManager"proxy-target-class="true"/>

<tx:advice id="txAdvice"  >

<tx:attributes>

<tx:method name="add"/>

</tx:attributes>

</tx:advice>

<aop:config >

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

<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>

</aop:config>

</beans>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值