Spring @AspectJ 实现AOP 入门例子(转)

时间:2014-07-02 09:55:35   收藏:0   阅读:220

AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子。 

引用一个猴子偷桃,守护者守护果园抓住猴子的小情节。 

1、猴子偷桃类(普通类): 

Java代码  bubuko.com,布布扣
  1. package com.samter.common;  
  2.   
  3. /** 
  4.  * 猴子 
  5.  * @author Administrator 
  6.  * 
  7.  */  
  8. public class Monkey {  
  9.       
  10.     public void stealPeaches(String name){  
  11.         System.out.println("【猴子】"+name+"正在偷桃...");  
  12.     }  
  13. }  



2、守护者类(声明为Aspect): 

Java代码  bubuko.com,布布扣
  1. package com.samter.aspect;  
  2.   
  3. import org.aspectj.lang.annotation.AfterReturning;  
  4. import org.aspectj.lang.annotation.Aspect;  
  5. import org.aspectj.lang.annotation.Before;  
  6. import org.aspectj.lang.annotation.Pointcut;  
  7.   
  8. /** 
  9.  * 桃园守护者 
  10.  * @author Administrator 
  11.  * 
  12.  */  
  13.   
  14. @Aspect  
  15. public class Guardian {  
  16.       
  17.     @Pointcut("execution(* com.samter.common.Monkey.stealPeaches(..))")  
  18.     public void foundMonkey(){}  
  19.   
  20.     @Before(value="foundMonkey()")  
  21.     public void foundBefore(){  
  22.         System.out.println("【守护者】发现猴子正在进入果园...");  
  23.     }  
  24.       
  25.     @AfterReturning("foundMonkey() && args(name,..)")  
  26.     public void foundAfter(String name){  
  27.         System.out.println("【守护者】抓住了猴子,守护者审问出了猴子的名字叫“"+name+"”...");  
  28.     }  
  29.       
  30. }  



3、XML配置文件: 

Java代码  bubuko.com,布布扣
  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"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"  
  5.        xsi:schemaLocation="  
  6.        http://www.springframework.org/schema/beans  
  7.        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  8.        http://www.springframework.org/schema/aop  
  9.        http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"  
  10. >  
  11.   
  12.     <!-- 定义Aspect -->  
  13.     <bean id="guardian" class="com.samter.aspect.Guardian" />  
  14.   
  15.     <!-- 定义Common -->  
  16.     <bean id="monkey" class="com.samter.common.Monkey" />  
  17.   
  18.     <!-- 启动AspectJ支持 -->  
  19.     <aop:aspectj-autoproxy />  
  20.   
  21. </beans>  



4、测试类: 

Java代码  bubuko.com,布布扣
  1. package com.samter.common;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class Main {  
  7.   
  8.     public static void main(String[] args) {  
  9.         ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");  
  10.         Monkey monkey = (Monkey) context.getBean("monkey");  
  11.         try {  
  12.             monkey.stealPeaches("孙大圣的大徒弟");  
  13.         }  
  14.         catch(Exception e) {}  
  15.     }  
  16.   
  17. }  



5、控制台输出: 

Java代码  bubuko.com,布布扣
  1. 【守护者】发现猴子正在进入果园...  
  2. 【猴子】孙大圣的大徒弟正在偷桃...  
  3. 【守护者】抓住了猴子,守护者审问出了猴子的名字叫“孙大圣的大徒弟”...  



解说:1写了一个猴子正在偷桃的方法。 
      2写了一个标志为@Aspect的类,它是守护者。它会在猴子偷桃之前发现猴子,并在猴子偷桃之后抓住猴子。 
      原理:A、@Aspect的声明表示这是一个切面类。 
            B、@Pointcut使用这个方法可以将com.samter.common.Monkey.stealPeaches(..)方法声明为poincut即切入点。作用,在stealPeaches方法被调用的时候执行2的foundMonkey方法。其中execution是匹配方法执行的切入点,也就是spring最常用的切入点定义方式。 
            C、@Before(value="foundMonkey()"):@Before声明为在切入点方法执行之前执行,而后面没有直接声明切入点,而是value="foundMonkey()",是因为如果@afterReturning等都有所改动的时候都必须全部改动,所以统一用Pointcut的foundMonkey代替,这样子有改动的时候仅需改动一个地方。其他@AfterReturning类同。 
      3是xml配置文件,里面有具体的注释。 

特别说明:Guardian类里面的@Pointcut("execution(* com.samter.common.Monkey.stealPeaches(..))"),如果stealPeaches有参数则..表示所有参数,@AfterReturning("foundMonkey() && args(name,..)")的&& args(name,..)可以获取切入点方法stealPeaches的参数。 

总结:这里列举了一个简单的例子,但是不难引申到应用中,当你写一个登陆系统的时候,你或许要记录谁成功登陆了系统,谁登陆系统密码错误等等的信息,这样子你用切面是再合适不过的了,总之当你的事务逻辑都设计到日志、安全检查、事务管理等等共同的内容的时候,用切面是要比你没有一个事务逻辑类都有相关代码或者相关引用好得多。 

2011-08-04补充: 
今天给筒子们上课,补充了一点。就是@Pointcut切入点的时候,我们改写一下正则表达式,修改为“@Pointcut("execution(* steal*(..))")”,意思就是说,针对工程下面所有的类以steal开头的方法都监控。即:我再添加一个大象类,它去偷香蕉(stealBanana)也会被抓。但是猴子类再加一个lookPeaches方法(看桃子)的话,就不会被抓。 

源:http://samter.iteye.com/blog/410618

Spring @AspectJ 实现AOP 入门例子(转),布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!