Spring 使用AOP导致IOC注入失败
2013-05-10 10:55
411 查看
前几天把系统重构了,服务层针对前后台分别提供相应的接口,但在配置注入的时候出现如下错误:
java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0] to required type [com.test.dao.UserDAO] for property 'dao': no matching editors or conversion strategy found
Spring配置如下:
上面的英文意思说无法将一个实现 com.test.dao.UserDAO 接口的代理注入给 dao
之所以会出现这个问题是:因为我配置了aop拦截了dao对象,Spring依照dao对象生成其接口的代理,当把这接口的代理塞给dao对象出错了
解决方法:AOP配置的拦截 避开dao对象
更改Spring的AOP配置:
java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy0] to required type [com.test.dao.UserDAO] for property 'dao': no matching editors or conversion strategy found
Spring配置如下:
<!-- 事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> <!-- <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception,SmoException,BmoException,DaoException" /> <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception,SmoException,BmoException,DaoException" /> <tx:method name="upd*" propagation="REQUIRED" rollback-for="Exception,SmoException,BmoException,DaoException" /> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> --> </tx:attributes> </tx:advice> <!-- Spring AOP config 解释一下(* com.evan.crm.service.*.*(..))中几个通配符的含义: 第一个 * —— 通配 任意返回值类型 第二个 * —— 通配 包com.evan.crm.service下的任意class 第三个 * —— 通配 包com.evan.crm.service下的任意class的任意方法 第四个 .. —— 通配 方法可以有0个或多个参数 --> <aop:config> <aop:pointcut id="servicesPointcut" expression="execution(* com.test..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicesPointcut" /> </aop:config>
上面的英文意思说无法将一个实现 com.test.dao.UserDAO 接口的代理注入给 dao
之所以会出现这个问题是:因为我配置了aop拦截了dao对象,Spring依照dao对象生成其接口的代理,当把这接口的代理塞给dao对象出错了
解决方法:AOP配置的拦截 避开dao对象
更改Spring的AOP配置:
<!-- 事务通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> <!-- <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception,SmoException,BmoException,DaoException" /> <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception,SmoException,BmoException,DaoException" /> <tx:method name="upd*" propagation="REQUIRED" rollback-for="Exception,SmoException,BmoException,DaoException" /> <tx:method name="*" propagation="SUPPORTS" read-only="true" /> --> </tx:attributes> </tx:advice> <!-- Spring AOP config 解释一下(* com.evan.crm.service.*.*(..))中几个通配符的含义: 第一个 * —— 通配 任意返回值类型 第二个 * —— 通配 包com.evan.crm.service下的任意class 第三个 * —— 通配 包com.evan.crm.service下的任意class的任意方法 第四个 .. —— 通配 方法可以有0个或多个参数 --> <aop:config> <aop:pointcut id="servicesPointcut" expression="execution(* com.test.main.TestFascade.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="servicesPointcut" /> </aop:config>
相关文章推荐
- Spring 使用AOP导致IOC注入失败
- springboot使用aop拦截controller干一些事导致service们@Autowired全部注入失败
- 利用springaop导致基于实现类注入失败的原因及解决方案
- 因Spring AOP导致@Autowired依赖注入失败的解决方法
- SpringAOP导致@Autowired依赖注入失败
- 初学spring 关于IOC与AOP 使用的设计模式
- Spring AOP切面类依赖注入失败问题解决
- spring使用aop时需要设置proxy-target-class="true" 否则无法依赖注入
- spring使用aop时需要设置proxy-target-class="true" 否则无法依赖注入
- spring 框架中的依赖注入(IOC--设值注入)---使用xml简单配置文件---的具体实例的简单实现
- spring 框架中的依赖注入(IOC--设值注入)--使用注解--的具体实例的简单实现
- 几个问题(十)-------spring的IOC、AOP的使用场景
- SpringAOP导致@Autowired依赖注入失败
- Spring--IoC--域属性的注入-使用@Resource注解(JSR)
- 使用spring的aop实现权限拦截后出现依赖注入为空的问题
- 使用spring的aop对Struts2的Action拦截后出现依赖注入为空问题
- 使用spring的aop实现拦截action后出现依赖注入为空的问题
- .NET平台下的依赖注入(IOC)和 面向切面(AOP)相结合简洁实用的使用方式
- 使用Service描述的服务与springAop冲突导致服务无法暴露
- spring使用aop时需要设置proxy-target-class="true" 否则无法依赖注入