Spring3.0中的前置通知、后置通知、环绕通知、异常通知

观众类Audience~~

  1. package com.jCuckoo.demo;  
  2.   
  3. import org.aspectj.lang.ProceedingJoinPoint;  
  4.   
  5. public class Audience {  
  6.     public void takeSeats() {  
  7.         System.out.println("----开演之前,请占座----");  
  8.     }  
  9.   
  10.     public void turnOffCellPhones() {  
  11.         System.out.println("----开始之前,请关机----");  
  12.     }  
  13.   
  14.     public void applaud() {  
  15.         System.out.println("****鼓掌,继续鼓掌。****");  
  16.     }  
  17.     public void turnOnCellPhones() {  
  18.         System.out.println("****演出结束,可以开机****");  
  19.     }  
  20.   
  21.     public void demandRefund() {  
  22.         System.out.println("
    退
    ");  
  23.     }  
  24.   
  25.     public void watchPerformance(ProceedingJoinPoint joinpoint) {  
  26.         try {  
  27.             System.out.println("oooooooo环绕通知开始oooooooo");  
  28.             long start = System.currentTimeMillis();  
  29.             joinpoint.proceed();  
  30.             long end = System.currentTimeMillis();  
  31.             System.out.println("oooooooo环绕通知结束oooooooo");  
  32.             System.out.println("演出耗时共计:" + (end - start)  
  33.                     + "毫秒。");  
  34.         } catch (Throwable t) {  
  35.             System.out.println("Boo!Wewantourmoneyback!");  
  36.         }  
  37.     }  
  38. }  


表演接口Performer

  1. package com.jCuckoo.demo;  
  2.   
  3. public interface Performer {  
  4.     void perform()throws Exception;  
  5. }  


定义魔术师Juggler,实现表演接口

  1. package com.jCuckoo.demo;  
  2.   
  3. public class Juggler implements Performer {  
  4.     private int beanBags = 3;  
  5.   
  6.     public Juggler() {  
  7.     }  
  8.   
  9.     public Juggler(int beanBags) {  
  10.         this.beanBags = beanBags;  
  11.     }  
  12.   
  13.     public void perform() throws Exception {  
  14.         System.out.println("表演开始:魔术师欺骗了" + beanBags + "个游戏豆。");  
  15.     }  
  16. }  


spring的配置文档applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="  
  6. http://www.springframework.org/schema/beans  
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8. http://www.springframework.org/schema/tx  
  9. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
  10. http://www.springframework.org/schema/aop  
  11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
  12.     <bean id="juggler" class="com.jCuckoo.demo.Juggler"/>  
  13.     <bean id="audience" class="com.jCuckoo.demo.Audience" />  
  14.     <aop:config>  
  15.         <aop:aspect ref="audience">  
  16.             <aop:pointcut id="performance"  
  17.                 expression="execution(* com.jCuckoo.demo.Performer.perform(..))" />  
  18.             <aop:before pointcut-ref="performance" method="takeSeats" />  
  19.             <aop:before pointcut-ref="performance" method="turnOffCellPhones" />  
  20.             <aop:after pointcut-ref="performance" method="turnOnCellPhones" />  
  21.             <aop:after-returning pointcut-ref="performance"  
  22.                 method="applaud" />  
  23.             <aop:after-throwing pointcut-ref="performance"  
  24.                 method="demandRefund" />  
  25.             <aop:around pointcut-ref="performance" method="watchPerformance"/>  
  26.         </aop:aspect>  
  27.     </aop:config>  
  28. </beans>  


测试类,获取魔术师,并进行表演。

  1. package com.jCuckoo.test;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import com.jCuckoo.demo.Performer;  
  7.   
  8. public class MainTest {  
  9.   
  10.     /** 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.         ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");  
  15.         Performer performer=(Performer)ctx.getBean("juggler");  
  16.         try {  
  17.             performer.perform();  
  18.         } catch (Exception e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.   
  22.     }  
  23.   
  24. }  


最终结果:

 

----开演之前,请占座----
----开始之前,请关机----
oooooooo环绕通知开始oooooooo
表演开始:魔术师欺骗了3个游戏豆。
****演出结束,可以开机****
****鼓掌,继续鼓掌。****
oooooooo环绕通知结束oooooooo
演出耗时共计:1毫秒。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值