Spring AOP 面向切面编程 常见通知实现(前置,后置,环绕,异常)

概述:在spring加载时,定义先后执行的方法,以及环绕异常等方法

观众类Audience~~

    package com.jCuckoo.demo;  
      
    import org.aspectj.lang.ProceedingJoinPoint;  
      
    public class Audience {  
        public void takeSeats() {  
            System.out.println("----开演之前,请占座----");  
        }  
      
        public void turnOffCellPhones() {  
            System.out.println("----开始之前,请关机----");  
        }  
      
        public void applaud() {  
            System.out.println("****鼓掌,继续鼓掌。****");  
        }  
        public void turnOnCellPhones() {  
            System.out.println("****演出结束,可以开机****");  
        }  
      
        public void demandRefund() {  
            System.out.println("
    太熊了,退我票钱
    ");  
        }  
      
        public void watchPerformance(ProceedingJoinPoint joinpoint) {  
            try {  
                System.out.println("oooooooo环绕通知开始oooooooo");  
                long start = System.currentTimeMillis();  
                joinpoint.proceed();  
                long end = System.currentTimeMillis();  
                System.out.println("oooooooo环绕通知结束oooooooo");  
                System.out.println("演出耗时共计:" + (end - start)  
                        + "毫秒。");  
            } catch (Throwable t) {  
                System.out.println("Boo!Wewantourmoneyback!");  
            }  
        }  
    }  

 表演接口Performer

    package com.jCuckoo.demo;  
      
    public interface Performer {  
        void perform()throws Exception;  
    }  

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

    package com.jCuckoo.demo;  
      
    public class Juggler implements Performer {  
        private int beanBags = 3;  
      
        public Juggler() {  
        }  
      
        public Juggler(int beanBags) {  
            this.beanBags = beanBags;  
        }  
      
        public void perform() throws Exception {  
            System.out.println("表演开始:魔术师欺骗了" + beanBags + "个游戏豆。");  
        }  
    }  

spring的配置文档applicationContext.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"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xsi:schemaLocation="  
    http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-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/aop  
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
        <bean id="juggler" class="com.jCuckoo.demo.Juggler"/>  
        <bean id="audience" class="com.jCuckoo.demo.Audience" />  
        <aop:config>  
            <aop:aspect ref="audience">  
                <aop:pointcut id="performance"  
                    expression="execution(* com.jCuckoo.demo.Performer.perform(..))" />  
                <aop:before pointcut-ref="performance" method="takeSeats" />  
                <aop:before pointcut-ref="performance" method="turnOffCellPhones" />  
                <aop:after pointcut-ref="performance" method="turnOnCellPhones" />  
                <aop:after-returning pointcut-ref="performance"  
                    method="applaud" />  
                <aop:after-throwing pointcut-ref="performance"  
                    method="demandRefund" />  
                <aop:around pointcut-ref="performance" method="watchPerformance"/>  
            </aop:aspect>  
        </aop:config>  
    </beans>  

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

    package com.jCuckoo.test;  
      
    import org.springframework.context.ApplicationContext;  
    import org.springframework.context.support.ClassPathXmlApplicationContext;  
      
    import com.jCuckoo.demo.Performer;  
      
    public class MainTest {  
      
        /** 
         * @param args 
         */  
        public static void main(String[] args) {  
            ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");  
            Performer performer=(Performer)ctx.getBean("juggler");  
            try {  
                performer.perform();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
      
        }  
      
    }  

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

作者原文出处:http://blog.csdn.net/guoquanyou/article/details/6754916

猜你喜欢

转载自zliguo.iteye.com/blog/2235276