循序渐进之Spring AOP(6) - 使用@Aspect注解
前面几节的示例看起来让人沮丧,要记忆如此多的接口、类和继承关系,做各种复杂的配置。好在这些只是一种相对过时的实现方式,现在只需要使用@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注解相关推荐
- Spring AOP示例教程 - Aspect,Advice,Pointcut,JoinPoint,Annotations,XML Configuration
Spring AOP示例教程 - Aspect,Advice,Pointcut,JoinPoint,Annotations,XML Configuration Spring Framework是基于两 ...
- Spring Aop实例之AspectJ注解配置
上篇博文<Spring Aop实例之xml配置>中,讲解了xml配置方式,今天来说说AspectJ注解方式去配置spring aop. 依旧采用的jdk代理,接口和实现类代码请参考上篇博文 ...
- SSM3==理解静态代理、动态代理Proxy.newProxyInstance、cglib代理==通过纯XML配置spring AOP,通过纯注解配置spring AOP
静态代理: 为什么要代理?在不改动原代码的基础上,丰富调用某个方法时实现的功能. 比如service类中原本update只会更新,但是通过代理类加上了判断权限和输出时间的功能. 其实这些功能也可以写在 ...
- Spring AOP XML配置及注解配置
一.XML配置切面类 1.日志切面类 //切面类注解配置加两个注解 @Aspect @Component public class LoggerAspect {public Object log(Pr ...
- spring aop中使用@Pointcut注解抽取可重用的切入点表达式
针对同一个切入点,如果有多个通知方法将切入时,通常我们需要标注多个注解,每个注解需要使用切入点表达式,告诉spring,当前通知方法在何时执行.当一个切入点需要更改时,就需要同时更改多个.为了避免这种 ...
- Java AOP开发模式@Aspect注解说明
2 注解说明 2.1 @Aspect 作用是把当前类标识为一个切面供容器读取 2.2 @Before 标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有 2.3 @AfterR ...
- Java AOP研究之@Aspect注解的工作原理
Created by Wang, Jerry, last modified on Jul 25, 2016
- spring AOP注解含义
spring AOP 的几个注解含义 @Aspect 指定一个类为切面类 @P ...
- aop注解配置切点 spring_使用注解匹配Spring Aop切点表达式
Spring中的类基本都会标注解,所以使用注解匹配切点可以满足绝大部分需求 主要使用@within()/@target @annotaton() @args()等... 匹配@Service类中的所有 ...
- Spring AOP小记
Spring AOP vs AspectJ 本文对于学习没什么参考性,写的也比较杂乱,是在学习过程中的一个笔记,有地方可能还串不起来,个人在学习时的一些关键点记录. AOP keywords Aspe ...
最新文章
- Java 2程序设计,Java2程序设计
- 性能测试相关名词解释
- Windows常用软件列表(2012.03 Update)
- 数据库开发——MySQL——存储引擎
- 朴素贝叶斯法---朴素贝叶斯法的参数估计
- mov eax,dword ptr fs:[0] 指令
- 【SSH高速进阶】——struts2简单的实例
- php 变量 换行,php去除换行符的方法小结(PHP_EOL变量的使用)_PHP教程
- H1作业(字符串和字节串)
- Android OpenGL ES 应用(二) 纹理
- iOS手势学习UIGestureRecognizer cocos2d 手势推荐
- Web开发之django(二Admin)
- Codeforces Round #226 (Div. 2)
- 6.Shell 编程从入门到精通 --- 文件和文件系统
- PAIP.pdf使用
- 备考OCJP认证知识点总结(四)
- Junit 4 的使用
- 计算机网络技术毕业生实习报告_20XX计算机网络技术毕业生实习报告1000字
- 构造常数函数解决抽象函数导数小题
- 数据库设计(1)_概念结构设计