前面几节的示例看起来让人沮丧,要记忆如此多的接口、类和继承关系,做各种复杂的配置。好在这些只是一种相对过时的实现方式,现在只需要使用@Aspect注解及表达式就可以轻松的使用POJO来定义切面,设计精妙正如Spring MVC的@Controller。

1 示例

仍然使用上一节的"骑士和剑士"的例子,目标类Horseman和Swordman

public class Horseman {
public void rush(String enemy) {
System.out.println(this.getClass().getSimpleName() + "冲刺攻击" + enemy);
} public void chop(String enemy) {
System.out.println(this.getClass().getSimpleName() + "砍劈攻击" + enemy);
}
}
public class Swordman {
public void block(String enemy) {
System.out.println(this.getClass().getSimpleName() + "格挡" + enemy);
} public void chop(String enemy) {
System.out.println(this.getClass().getSimpleName() + "砍劈攻击" + enemy);
}
}

切面

@Aspect
public class StorageAdvisor { @Before("execution(* chop(..))")
public void beforeAttack(JoinPoint point) {
System.out.println("Advice: " + point.getTarget().getClass().getSimpleName() + "蓄力");
}
}

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <aop:aspectj-autoproxy />
<bean id="horseman" class="examples.chap03.Horseman" />
<bean id="swordman" class="examples.chap03.Swordman" />
<bean class="examples.chap03.StorageAdvisor" />
</beans>

测试代码

	public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("examples/chap03/applicationContext.xml");
Horseman hm = (Horseman)context.getBean("horseman");
hm.rush("Ghoul");
hm.chop("Ghoul");
Swordman sm = (Swordman)context.getBean("swordman");
sm.block("Ghoul");
sm.chop("Ghoul");
}

对比上一节的内容可以看到,实现同样的功能,代码和配置都精简了很多,而且构建切面的类是一个普通的POJO,开发和测试的灵活性都大幅度提高。

2 注解说明

2.1 @Aspect

作用是把当前类标识为一个切面供容器读取

2.2 @Before

标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有

2.3 @AfterReturning

后置增强,相当于AfterReturningAdvice,方法正常退出时执行

2.4 @AfterThrowing

异常抛出增强,相当于ThrowsAdvice

2.5 @After

final增强,不管是抛出异常或者正常退出都会执行

2.6 @Around

环绕增强,相当于MethodInterceptor

2.7 @DeclareParents

引介增强,相当于IntroductionInterceptor

3 execution切点函数

execution函数用于匹配方法执行的连接点,语法为:

execution(方法修饰符(可选)  返回类型  方法名  参数  异常模式(可选))

参数部分允许使用通配符:

*  匹配任意字符,但只能匹配一个元素

.. 匹配任意字符,可以匹配任意多个元素,表示类时,必须和*联合使用

+  必须跟在类名后面,如Horseman+,表示类本身和继承或扩展指定类的所有类

示例中的* chop(..)解读为:

方法修饰符  无

返回类型      *匹配任意数量字符,表示返回类型不限

方法名          chop表示匹配名称为chop的方法

参数               (..)表示匹配任意数量和类型的输入参数

异常模式       不限

更多示例:

void chop(String,int)

匹配目标类任意修饰符方法、返回void、方法名chop、带有一个String和一个int型参数的方法

public void chop(*)

匹配目标类public修饰、返回void、方法名chop、带有一个任意类型参数的方法

public String *o*(..)

匹配目标类public修饰、返回String类型、方法名中带有一个o字符、带有任意数量任意类型参数的方法

public void *o*(String,..)

匹配目标类public修饰、返回void、方法名中带有一个o字符、带有任意数量任意类型参数,但第一个参数必须有且为String型的方法

也可以指定类:

public void examples.chap03.Horseman.*(..)

匹配Horseman的public修饰、返回void、不限方法名、带有任意数量任意类型参数的方法

public void examples.chap03.*man.*(..)

匹配以man结尾的类中public修饰、返回void、不限方法名、带有任意数量任意类型参数的方法

指定包:

public void examples.chap03.*.chop(..)

匹配examples.chap03包下所有类中public修饰、返回void、方法名chop、带有任意数量任意类型参数的方法

public void examples..*.chop(..)

匹配examples.包下和所有子包中的类中public修饰、返回void、方法名chop、带有任意数量任意类型参数的方法

可以用这些表达式替换StorageAdvisor中的代码并观察效果

4 更多切点函数

除了execution(),Spring中还支持其他多个函数,这里列出名称和简单介绍,以方便根据需要进行更详细的查询

4.1 @annotation()

表示标注了指定注解的目标类方法

例如 @annotation(org.springframework.transaction.annotation.Transactional) 表示标注了@Transactional的方法

4.2 args()

通过目标类方法的参数类型指定切点

例如 args(String) 表示有且仅有一个String型参数的方法

4.3 @args()

通过目标类参数的对象类型是否标注了指定注解指定切点

如 @args(org.springframework.stereotype.Service) 表示有且仅有一个标注了@Service的类参数的方法

4.4 within()

通过类名指定切点

如 with(examples.chap03.Horseman) 表示Horseman的所有方法

4.5 target()

通过类名指定,同时包含所有子类

如 target(examples.chap03.Horseman)  且Elephantman extends Horseman,则两个类的所有方法都匹配

4.6 @within()

匹配标注了指定注解的类及其所有子类

如 @within(org.springframework.stereotype.Service) 给Horseman加上@Service标注,则Horseman和Elephantman 的所有方法都匹配

4.7 @target()

所有标注了指定注解的类

如 @target(org.springframework.stereotype.Service) 表示所有标注了@Service的类的所有方法

4.8 this()

大部分时候和target()相同,区别是this是在运行时生成代理类后,才判断代理类与指定的对象类型是否匹配

5 逻辑运算符

表达式可由多个切点函数通过逻辑运算组成

5.1 &&

与操作,求交集,也可以写成and

例如 execution(* chop(..)) && target(Horseman)  表示Horseman及其子类的chop方法

5.2 ||

或操作,求并集,也可以写成or

例如 execution(* chop(..)) || args(String)  表示名称为chop的方法或者有一个String型参数的方法

5.3 !

非操作,求反集,也可以写成not

例如 execution(* chop(..)) and !args(String)  表示名称为chop的方法但是不能是只有一个String型参数的方法

循序渐进之Spring AOP(6) - 使用@Aspect注解的更多相关文章

  1. Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解

    引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...

  2. Spring AOP(5)-- 注解

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...

  3. Spring AOP配置简单记录(注解及xml配置方式)

    在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...

  4. 【Spring AOP】Spring AOP之如何通过注解的方式实现各种通知类型的AOP操作进阶篇(3)

    一.切入点表达式的各种类型 切入点表达式的作用:限制连接点的匹配(满足时对应的aspect方法会被执行) 1)execution:用于匹配方法执行连接点.Spring AOP用户可能最经常使用exec ...

  5. spring AOP (使用AspectJ的注解方式 的aop实现) (6)

    目录 一.在 Spring 中启用 AspectJ 注解支持 二.AspectJ 支持 5 种类型的通知注解: 2.1.使用之前的 计算器接口和实现类 ArithmeticCalculator.jav ...

  6. 循序渐进之Spring AOP(3) - 配置代理

    上一篇介绍了几种Advice(增强),并通过代码演示了生成代理的方式,下面来看通过配置文件配置方式把Advice织入目标类. 注意,配置文件方式仍然不是spring AOP的最好方式,学习配置方式也是 ...

  7. 循序渐进之Spring AOP(1) - 原理

    AOP全称是Aspect Oriented Programing,通常译为面向切面编程.利用AOP可以对面向对象编程做很好的补充. 用生活中的改装车比喻,工厂用面向对象的方法制造好汽车后,车主往往有些 ...

  8. Spring/AOP框架, 以及使用注解

    1, 使用代理增加日志, 也是基于最原始的办法 import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; ...

  9. 循序渐进之Spring AOP(5) - 创建切面

    在掌握了可用的增强后,接下来要做的就是精确的描述切点.前面的示例都是指定一个目标类并把增强织入到所有方法中,实际开发显然会有更精细的筛选需求,比如对所有类中名称以test结尾的方法加入监控执行时间,或 ...

随机推荐

  1. 网页闯关游戏(riddle webgame)--SQL注入的潘多拉魔盒

    前言: 之前编写了一个网页闯关游戏(类似Riddle Game), 除了希望大家能够体验一下我的游戏外. 也愿意分享编写这个网页游戏过程中, 学到的一些知识. web开发初学者往往会忽视一些常见的漏洞 ...

  2. OJ上 G++ 与 C++ 的区别

    1.输出double类型时,如果采用G++提交,scanf采用%lf,prinf采用%f,否则会报错 2.使用GCC/G++的提醒: 对于64位整数, long long int 和 __int64 ...

  3. 如何让WordPress支持上传更多文件类型

    如何让WordPress支持上传更多文件类型   可以在functions.php中这样写: 1 2 3 4 5 6 7 8 9 add_filter('upload_mimes', 'wpdit_f ...

  4. Event List 2

    The list of events can be found in src/switch_event.c in a char array called EVENT_NAMES and is summ ...

  5. 将数据集做成VOC2007格式用于Faster-RCNN训练

    1.命名 文件夹名VOC2007.图片名六位数字.将数据集相应的替换掉VOC2007中的数据. (Updated development kit, annotated test data )   2. ...

  6. input:-webkit-autofill 导致chrome的输入框背景颜色变成黄色

    填写form表单时发现chrome的一个好坑啊! 当你之前有填写过表单,获取焦点时,input会有一个记录之前填写过的文本的下拉列表式的东东,就像这样:(抱歉丑了点,隐私问题打上了马赛克) 按理说,这 ...

  7. docker swarm compose

    swarm docker run swarm --help compose curl -L https://github.com/docker/compose/releases/download/1. ...

  8. winform中dataGridView高度自适应填充完数据的高度

    // winform中dataGridView高度自适应填充完数据的高度,就是dataGridView自身不产生滚动条,自己的高度是根据数据的多少而变动. 在load的时候,数据绑定后,加上如下代码: ...

  9. 二、Spring——AoP

    AOP概述 AOP是OOP的有益补充,他为程序开发提供了一个崭新的思考角度,可以将重复性的横切逻辑抽取到统一的模块中,通过OOP的纵向抽象和AOP的横向抽取,程序才能真正解决复杂性代码问题. Spri ...

  10. Tomcat应用中post方式传参数长度限制

    Tomcat应用中post方式传参数长度限制 jsp页面上是没有限制的,但是在tomcat服务器上有限制,Tomcat 默认的post参数的最大大小为2M, 当超过时将会出错,可以配置maxPostS ...