Spring @AspectJ 实现AOP 入门例子(转)
AOP的作用这里就不再作说明了,下面开始讲解一个很简单的入门级例子。
引用一个猴子偷桃,守护者守护果园抓住猴子的小情节。
1、猴子偷桃类(普通类):
- package com.samter.common;
- /**
- * 猴子
- * @author Administrator
- *
- */
- public class Monkey {
- public void stealPeaches(String name){
- System.out.println("【猴子】"+name+"正在偷桃...");
- }
- }
2、守护者类(声明为Aspect):
- package com.samter.aspect;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- /**
- * 桃园守护者
- * @author Administrator
- *
- */
- @Aspect
- public class Guardian {
- @Pointcut("execution(* com.samter.common.Monkey.stealPeaches(..))")
- public void foundMonkey(){}
- @Before(value="foundMonkey()")
- public void foundBefore(){
- System.out.println("【守护者】发现猴子正在进入果园...");
- }
- @AfterReturning("foundMonkey() && args(name,..)")
- public void foundAfter(String name){
- System.out.println("【守护者】抓住了猴子,守护者审问出了猴子的名字叫“"+name+"”...");
- }
- }
3、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"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
- >
- <!-- 定义Aspect -->
- <bean id="guardian" class="com.samter.aspect.Guardian" />
- <!-- 定义Common -->
- <bean id="monkey" class="com.samter.common.Monkey" />
- <!-- 启动AspectJ支持 -->
- <aop:aspectj-autoproxy />
- </beans>
4、测试类:
- package com.samter.common;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
- Monkey monkey = (Monkey) context.getBean("monkey");
- try {
- monkey.stealPeaches("孙大圣的大徒弟");
- }
- catch(Exception e) {}
- }
- }
5、控制台输出:
- 【守护者】发现猴子正在进入果园...
- 【猴子】孙大圣的大徒弟正在偷桃...
- 【守护者】抓住了猴子,守护者审问出了猴子的名字叫“孙大圣的大徒弟”...
解说: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 入门例子(转)的更多相关文章
- Spring 3.0 Aop 入门
关于Aop的原理,动态代理,反射,之类的底层java技术网上搜一堆一堆的..我就不多说了,主要说在spring上使用aop的方法. 首先不得不说一下的就是,spring aop的支持需要外部依赖包: ...
- Spring 注解式Aop 入门
首先在spring配置文件中加上 xmlns:aop="http://www.springframework.org/schema/aop" http://www.springfr ...
- Spring入门(二)— IOC注解、Spring测试、AOP入门
一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...
- Spring框架(6)---AspectJ实现AOP
AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...
- Spring基于AspectJ的AOP的开发——注解
源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...
- 8 -- 深入使用Spring -- 4...2 使用AspectJ实现AOP
8.4.2 使用AspectJ实现AOP AspectJ是一个基于Java语言的AOP框架.Spring 4.0 的AOP对AspectJ很好的集成. AspectJ是Java 语言的一个AOP实现, ...
- Spring AOP 学习例子
http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example 工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大 ...
- Spring AspectJ AOP 完整示例
http://outofmemory.cn/java/spring/AOP/aop-aspectj-example-before-after-AfterReturning-afterThrowing- ...
随机推荐
- Enum简单例子DropdownList
借鉴:http://www.cnblogs.com/suizhikuo/archive/2013/06/07/3125225.html vs2012 mvc4 最终效果: 1.建立mvc4 Inter ...
- 让 http 2来得更猛烈些吧
今早在公交车上,把http2的官方讲解文档(还在草案之中)看了一圈,发现相对http 1.1确实改进了不少,完整的文档可通过:://www.gitbook.com/book/ye11ow/http2- ...
- LeetCode 笔记系列 17 Largest Rectangle in Histogram
题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...
- MVC模式在UI里的应用
In a similar way to other parts of a game, user interfaces usually go through several iterations unt ...
- 在VS中用正则表达式查找或替换
2005VS和2008VS正则替换规则如下(2013VS不适合,不需要的同学可以直接跳到2013VS的操作办法): Example: 查找#incldue中带有gl开头的头文件的,用include.+ ...
- Android BLE 蓝牙编程(三)
上节我们已经可以连接上蓝牙设备了. 本节我们就要获取手环的电池电量和计步啦. 在介绍这个之前我们需要先了解下什么是 服务 什么是 UUID 我们记得上节中我们item监听事件的回调的返回值是Bluet ...
- iOS真机调试引入第三方库(如友盟等)编译时候,出现错误提示
用Xcode 7 beta 3在真机(iOS 8.3)上运行一下工程,结果发现工程编译不过.看了下问题,报的是以下错误: MARK:解决方法:在building Setting 中设置bitCode ...
- 【51Nod 1674】【算法马拉松 19A】区间的价值 V2
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1674 对区间分治,统计\([l,r]\)中经过mid的区间的答案. 我的 ...
- oracle 表字段添加 修改 删除语法
修改列名 alter table 表明 rename column rename 老列名 to 新列名添加 字段alter table 表名 add(字段名 类型):删除字段alter table 表 ...
- Python 面向对象 基础
编程范式概述:面向过程 和 面向对象 以及函数式编程 面向过程:(Procedure Oriented)是一种以事件为中心的编程思想. 就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现 ...