spring aop配置中的坑

spirng aop xml配置类型转换异常

场景:一场表演(Performance.perform())前后的观众行为(Audience类中定义的方法)

下面是Audience类的定义

package com.kylin.springaop.xml;

public class Audience {
    public void seatDown(){
        System.out.println("seat down");
    }

    public void claps(){
        System.out.println("claps claps claps");
    }

    public void returned(){
        System.out.println("returned");
    }

    public void exception(){
        System.out.println("exception");
    }
}

下面是Performance类

package com.kylin.springaop.xml;

public class Performance {
    public void performance(){
        System.out.println("perform");
    }
}

下面是xml配置

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="audience" class="com.kylin.springaop.xml.Audience"/>
    <bean id="performance" class="com.kylin.springaop.xml.Performance"/>

    <!--
        下面聊一个巨坑的东西,如果切点Bean的id和pointcut的id相同的话,就会出现
        Exception in thread "main" java.lang.ClassCastException:
        org.springframework.aop.aspectj.AspectJExpressionPointcut cannot be cast to com.kylin.springaop.xml.Performance
        spring将会认为上面配置的Bean是一个切点,然而切点是Bean中的perform()方法,就会出现
        类型转换异常。
    -->
    <!--错误配置,pointcut的id不能和Bean的id相同
    <aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut id="performance"
                          expression="execution(* com.kylin.springaop.xml.Performance.performance(..))"/>
            <aop:before method="seatDown" pointcut-ref="performance"/>
            <aop:after method="claps" pointcut-ref="performance"/>
            <aop:after-returning method="returned" pointcut-ref="performance"/>
            <aop:after-throwing method="exception" pointcut-ref="performance"/>
        </aop:aspect>
    </aop:config>
    -->
    <aop:config>
        <aop:aspect ref="audience">
            <aop:pointcut id="perform"
                          expression="execution(* com.kylin.springaop.xml.Performance.performance(..))"/>
            <aop:before method="seatDown" pointcut-ref="perform"/>
            <aop:after method="claps" pointcut-ref="perform"/>
            <aop:after-returning method="returned" pointcut-ref="perform"/>
            <aop:after-throwing method="exception" pointcut-ref="perform"/>
        </aop:aspect>
    </aop:config>
</beans>

总的来说切点的id不能和bean的id相同。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,923评论 18 139
  • 本章内容: 面向切面编程的基本原理 通过POJO创建切面 使用@AspectJ注解 为AspectJ切面注入依赖 ...
    谢随安阅读 3,193评论 0 9
  • PG ONE 小白 T-T VAVA Bridge and gai 全部都是我的菜 不好意思我就是这么博爱 ——诶...
    团子君坚持碎碎念阅读 426评论 0 0
  • 前段时间的热播剧《我的前半生》被评论的沸沸扬扬,从剧中角色到演员本人无一不被各种写手从各个角度拼命剖析。谁还记得:...
    君之语阅读 237评论 0 0
  • 摘一瓣流霞 留一瓣在穹苍 沉一半木桨 留一半在水中央 新荷上滚落的露珠 是昨夜上弦月的眼泪 风声吞没溪流的呜咽 吹...
    宋予屿阅读 351评论 3 10