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- ...
随机推荐
- Python list
序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见的是列表和元组. 序列 ...
- ZooKeeper 笔记(1) 安装部署及hello world
先给一堆学习文档,方便以后查看 官网文档地址大全: OverView(概述) http://zookeeper.apache.org/doc/r3.4.6/zookeeperOver.html Get ...
- 服务器一般达到多少qps比较好[转]
你好,每秒查询率QPS是对一个特定的查询服务器在规定时间内所处理流量多少的衡量标准,在因特网上,作为域名系统服务器的机器的性能经常用每秒查询率来衡量. 原理:每天80%的访问集中在20%的时间里,这2 ...
- C#和C实现通过CRC-16 (Modbus)获取CRC值并校验数据(代码)
文章首发于浩瀚先森博客 CRC的全称为Cyclic Redundancy Check,中文名称为循环冗余校验.它是一类重要的线性分组码,编码和解码方法简单,检错和纠错能力强,在通信领域广泛地用于实现差 ...
- jquery-自适应全屏背景轮播动画
实时自适应浏览器窗口大小的全屏背景轮播动画 <!DOCTYPE html> <html> <head> <meta http-equiv="Cont ...
- deepin 15.3添加PPA源 安装php5.6
想要在deepin 15.3上安装PHP5.6,我们需要手动添加源. 在https://launchpad.net/+search?field.text=php上可以通过搜索找到你想要的软件源, PP ...
- Java的多线程机制系列:(二)缓存一致性和CAS
一.总线锁定和缓存一致性 这是两个操作系统层面的概念.随着多核时代的到来,并发操作已经成了很正常的现象,操作系统必须要有一些机制和原语,以保证某些基本操作的原子性.首先处理器需要保证读一个字节或写一个 ...
- 【BZOJ-2669】局部极小值 状压DP + 容斥原理
2669: [cqoi2012]局部极小值 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 561 Solved: 293[Submit][Status ...
- PostgreSQL Apt Repository
PostgreSQL Apt Repository If the version included in your version of Ubuntu is not the one you want, ...
- Python Day10
进程 在python中multiprocess模块提供了Process类,实现进程相关的功能.但是,由于它是基于fork机制的,因此不被windows平台支持.想要在windows中运行,必须使用if ...