您的位置:首页 > 编程语言 > Java开发

spring AOP总结

2018-01-01 15:31 411 查看

AOP术语解释

Advice (通知)

切面的使用时机,spring aop有5种时机

before(方法调用前)

after (方法调用之后,不管是否成功)

after-running (方法调用成功后)

after-throwing(方法抛出异常)

around (包住整个方法,可以在通知方法执行前后添加自定义行为)

JoinPoint(连接点)

代码执行流程上的任意一点

pointcut (切点)

切面与代码流程的交叉点

aspect (切面)

需要做的事情。按照其他翻译比如面向方面编程会更直观理解。或者理解成模块。正好符合aop处理一些难以完全解耦的模块的初衷。例如随处必不可少的日志行为

weaving (织入)

切面应用到目标对象的时候创建proxy对象的过程。可以使用编译时生成代码,classloader,动态代理实现。spring aop采用的是动态代理

使用方法(注解的方式,不探究xml了)

织入点

pointcut编写

声明aspect

声明通知

采用spring in action的例子

注入点选择的是一个表演者,我们写一个Performer的类,里面包含一个表演的动作

package com.example.aspect

public class Performer {
public void perform() {
System.out.println("performer perform");
}

}


pointcut

下面编写pointcut,pointcut编写按照一定的规则:

execution指示器

execution(* com.example.aspect.Performer.perform(..))


指示器依次表达的是返回类型,方法,参数类型

*表示不关心返回类型, ..表示不关心入参类型

execution还支持逻辑运算符以及within()指示器限定包名

例如

execution (* com.example.aspect.Performer.performer(..) && within(com.example.aspect))


这段表达式的含义是满足execution的函数并且只能在com.example.aspect这个包下面使用。

&&也可以是||, within也支持!

aspect

编写一个切面,并加入刚刚的切点

分别使用@Aspect和@Pointcut注解

package com.example.aspect;

@Aspect
public class Audience {

@Pointcut("execution(* com.example.aspect.Performer.perform(..))")
public void performer() {
}
}


声明audience

这里仍然使用spring in action的例子,在pointcut前后添加前/后置通知

@AfterReturning("performer()")
public void takeSeats() {
System.out.println("The audience is taking their seats");
}

@Before("performer()")
public void turnOffCellPhones() {
System.out.println("The audience is turning off their cellphones");
}

@After("performer()")
public void applaud() {
System.out.println("CLAP CLAP CLAP CLAP CLAP");
}

@AfterThrowing("performer()")
public void demandRefund() {
System.out.println("Boo! We want out money back");
}


前置通知其实可以满足大部分切面的需求。但是如果我们需要加入一些自定义行为的时候,前置通知或者后置通知是很难满足需求的,例如我需要统计performer的时间,就不得不使用多余的全局变量。

around通知很好的解决了这个问题,around通知需要一个ProcceedingJoinPoint参数。具体代码如下:

@Around("performer()")
public void watchPerformer(ProceedingJoinPoint joinPoint) {
try {
long t1 = System.currentTimeMillis();
joinPoint.proceed();
long t2 = System.currentTimeMillis();
System.out.println(t2 - t1);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}


启用AOP

以上我们完成了关键步骤,还需要进行最后一些配置,就可以开始使用spring aop。这里我们要使用

@EnableAspectJAutoProxy注解,并且声明Bean

@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class AspectConfig {

@Bean
public Performer performer() {
return new Performer();
}

@Bean
public Audience audience() {
return new Audience();
}
}


下面测试下程序

public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AspectConfig.class);
Performer performer = applicationContext.getBean(Performer.class);
performer.perform();
}
}


运行结果如下:

The audience is turning off their cellphones
performer perform
61
CLAP CLAP CLAP CLAP CLAP
The audience is taking their seats


一些小思考

OOP虽然把模块拆解的很好,但是难以避免的有很多必需的业务会夹在在各个模块。例如日志,或者Android中的动态权限申请。这些样板代码写起来也非常的复杂。而AOP则可以很好的解耦对这些业务场景的处理
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring aop