例子同上一文
1.接口
package com.qhf.aop.example03;
public interface Performance {
public void perform();
}
2.表演实现类
package com.qhf.aop.example03;
public class PerformanceImpl implements Performance {
@Override
public void perform() {
//int a = 1/0;
System.out.println("表演过程中...");
}
}
3.观众类
package com.qhf.aop.example03;
import org.aspectj.lang.ProceedingJoinPoint;
public class Audience {
public void silencePhones(){
System.out.println("关手机...");
}
public void takeSeats(){
System.out.println("找座位...");
}
public void applause(){
System.out.println("鼓掌...");
}
public void demandRefund(){
System.out.println("要求重演...");
}
public void watchPerformance(ProceedingJoinPoint pj){
try {
silencePhones();
takeSeats();
pj.proceed();
applause();
} catch (Throwable t){
demandRefund();
t.printStackTrace();
}
}
}
4.配置类
package com.qhf.aop.example03;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration //作为配置文件之一
@ImportResource("classpath:aop/example03/aop.xml")
public class AOPConfig {
}
5.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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy/>
<!--<aop:config>-->
<!--<aop:aspect ref="audience">-->
<!--<!– 定义切面–>-->
<!--<aop:pointcut id="performance" expression="execution(* com.qhf.aop.example03.Performance.perform(..))"/>-->
<!--<!– 引用切面 –>-->
<!--<aop:before pointcut-ref="performance" method="silencePhones"/>-->
<!--<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>-->
<!-- 环绕通知 -->
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" expression="execution(* com.qhf.aop.example03.Performance.perform(..))"/>
<!-- 也需要定义watchPerformance 方法,使用ProceedingJoinPoint 参数的proceed() 方法 -->
<aop:around method="watchPerformance" pointcut-ref="performance"/>
</aop:aspect>
</aop:config>
<bean id="performanceImpl" class="com.qhf.aop.example03.PerformanceImpl"/>
<bean id="audience" class="com.qhf.aop.example03.Audience"/>
</beans>
6.测试
ackage com.qhf.aop.example03;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes= AOPConfig.class)
public class Test {
@Autowired
private Performance performance;//即便这里名字是performance,符合这个接口的实现类是PerformanceImpl,所以bean的id是performanceImpl
@org.junit.Test
public void test(){
performance.perform();
}
}
7.结果
关手机...
找座位...
表演过程中...
鼓掌...