zoukankan      html  css  js  c++  java
  • Spring 使用AOP导致IOC注入失败

    前几天把系统重构了,服务层针对前后台分别提供相应的接口,但在配置注入的时候出现如下错误:

    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>
  • 相关阅读:
    Testlink1.9.17使用方法( 第三章 初始配置[配置用户、产品] )
    Testlink1.9.17使用方法(第二章 登录&汉化设置)
    Testlink1.9.17使用方法(第一章 前言)
    TestLink-Windows安装教程
    Linux-Redmine安装方法
    怎么使用Fiddler进行抓包
    配置ADB到Windows环境变量
    Android获取定位权限,获取设备所在的经纬度
    Paint.FontMetrics
    Bitmap上下合成图片
  • 原文地址:https://www.cnblogs.com/macula/p/3070688.html
Copyright © 2011-2022 走看看