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相同。