1.无注解的Audience
package XMLconcert; public class Audience { public void silenceCellPhones() {
System.out.println("Silencing cell phone");
} public void takeSeats() {
System.out.println("Taking seats");
} public void applause() {
System.out.println("CLAP CLAP CLAP");
} public void demandRefund() {
System.out.println("Demanding a refund");
} }
2.通过XML将无注解的Audience声明为切面
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="audience" class="XMLconcert.Audience"></bean>
<bean class="XMLconcert.Classcial"></bean>
<aop:config>
<aop:aspect ref="audience">
<aop:before pointcut="execution(* XMLconcert.Performance.perform(..))" method="silenceCellPhones"/>
<aop:before pointcut="execution(* XMLconcert.Performance.perform(..))" method="takeSeats"/>
<aop:after-returning pointcut="execution(* XMLconcert.Performance.perform(..))" method="applause"/>
<aop:after-throwing pointcut="execution(* XMLconcert.Performance.perform(..))" method="demandRefund"/>
</aop:aspect>
</aop:config> </beans>
或者
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut expression="execution(* XMLconcert.Performance.perform(..))" id="performance"/>
<aop:before pointcut-ref="performance" method="silenceCellPhones"/>
<aop:before pointcut-ref="performance" method="takeSeats"/>
<aop:after-returning pointcut-ref="performance" method="applause"/>
<aop:after-throwing pointcut-ref="performance" method="demandRefund"/>
</aop:aspect>
</aop:config>
或者
替换Audience中的四个方法
public void watchPerformance(ProceedingJoinPoint jp) {
try {
System.out.println("Silencing cell phone");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP");
} catch (Throwable e) {
System.out.println("Demanding a refund");
}
}
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut expression="execution(* XMLconcert.Performance.perform(..))" id="performance"/>
<aop:around method="watchPerformance" pointcut-ref="performance"/>
</aop:aspect>
</aop:config>
3.结果