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

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

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

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

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

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

3、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"
  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. <!-- 定义Aspect -->
  12. <bean id="guardian" class="com.samter.aspect.Guardian" />
  13. <!-- 定义Common -->
  14. <bean id="monkey" class="com.samter.common.Monkey" />
  15. <!-- 启动AspectJ支持 -->
  16. <aop:aspectj-autoproxy />
  17. </beans>

4、测试类:

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

5、控制台输出:

  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 入门例子(转)的更多相关文章

  1. Spring 3.0 Aop 入门

    关于Aop的原理,动态代理,反射,之类的底层java技术网上搜一堆一堆的..我就不多说了,主要说在spring上使用aop的方法. 首先不得不说一下的就是,spring aop的支持需要外部依赖包: ...

  2. Spring 注解式Aop 入门

    首先在spring配置文件中加上 xmlns:aop="http://www.springframework.org/schema/aop" http://www.springfr ...

  3. Spring入门(二)— IOC注解、Spring测试、AOP入门

    一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...

  4. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  5. Spring框架(6)---AspectJ实现AOP

    AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...

  6. Spring基于AspectJ的AOP的开发——注解

    源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...

  7. 8 -- 深入使用Spring -- 4...2 使用AspectJ实现AOP

    8.4.2 使用AspectJ实现AOP AspectJ是一个基于Java语言的AOP框架.Spring 4.0 的AOP对AspectJ很好的集成. AspectJ是Java 语言的一个AOP实现, ...

  8. Spring AOP 学习例子

    http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example     工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大 ...

  9. Spring AspectJ AOP 完整示例

    http://outofmemory.cn/java/spring/AOP/aop-aspectj-example-before-after-AfterReturning-afterThrowing- ...

随机推荐

  1. Linux 进程间通讯详解三

    msgctl()函数 int msgctl(int msqid, int cmd, struct msqid_ds *buf); --参数 msqid:有msgget函数返回的消息队列标识码 cmd: ...

  2. NOSQL数据模型和CAP原理

    NOSQL数据模型和CAP原理 http://blog.sina.com.cn/s/blog_7800d9210100t33v.html 我本来一直觉得NoSQL其实很容易理解的,我本身也已经对NoS ...

  3. jquery submit() 提交失败

    今天写一个表单提交 居然走到$('#wechat_form').submit() 这,但怎么都没有提交这个表单 google 了一下 Additional Notes:Forms and their ...

  4. 使用Extjs组件实现Top-Left-Main布局并且增加事件响应

    每次在毕业答辩会上,看到同专业的同学只要是XXX管理系统,就是下图所示的界面,看来这中布局还是很受欢迎的(偷笑).接下来进入我们正题,在web项目无论是前端还是后台管理比较常见的布局就是Top-Lef ...

  5. ionic+angularjs开发hybrid App(环境配置+创建测试项目)

    本文使用的系统是win10 因为后期需要使用nodejs 所以先把node装好 https://nodejs.org/download/ 下载JDK并配置Java运行环境 http://www.ora ...

  6. the pipeline of call SNP

    ######################################## ############### Mapping ################ ################## ...

  7. Leetcode 230. Kth Smallest Element in a BST

    Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...

  8. 【BZOJ-3243】向量内积 随机化 + 矩阵

    3243: [Noi2013]向量内积 Time Limit: 10 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 1249  Solved:  ...

  9. 【bzoj2281】 Sdoi2011—黑白棋

    http://www.lydsy.com/JudgeOnline/problem.php?id=2281 (题目链接) 题意 一个1*n的棋盘,棋盘上一个隔一个的放着个黑棋和白棋,最左端是白棋,最右端 ...

  10. TYVJ1338 QQ农场

    时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 Sandytea前段时间沉迷于QQ农场中……一天夜里,他梦见来到好友X的农场上…… 描述 这个农场和游戏中略有 ...