spring AOP总结

AOP术语解释

  • Advice (通知)
    切面的使用时机,spring aop有5种时机
    before(方法调用前)
    after (方法调用之后,不管是否成功)
    after-running (方法调用成功后)
    after-throwing(方法抛出异常)
    around (包住整个方法,可以在通知方法执行前后添加自定义行为)
  • JoinPoint(连接点)
    代码执行流程上的任意一点
  • pointcut (切点)
    切面与代码流程的交叉点
  • aspect (切面)
    需要做的事情。按照其他翻译比如面向方面编程会更直观理解。或者理解成模块。正好符合aop处理一些难以完全解耦的模块的初衷。例如随处必不可少的日志行为
  • weaving (织入)
    切面应用到目标对象的时候创建proxy对象的过程。可以使用编译时生成代码,classloader,动态代理实现。spring aop采用的是动态代理

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

  1. 织入点
  2. pointcut编写
  3. 声明aspect
  4. 声明通知

采用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则可以很好的解耦对这些业务场景的处理

"sgmediation.zip" 是一个包含 UCLA(加利福尼亚大学洛杉矶分校)开发的 sgmediation 插件的压缩包。该插件专为统计分析软件 Stata 设计,用于进行中介效应分析。在社会科学、心理学、市场营销等领域,中介效应分析是一种关键的统计方法,它帮助研究人员探究变量之间的因果关系,尤其是中间变量如何影响因变量与自变量之间的关系。Stata 是一款广泛使用的统计分析软件,具备众多命令和用户编写的程序来拓展其功能,sgmediation 插件便是其中之一。它能让用户在 Stata 中轻松开展中介效应分析,无需编写复杂代码。 下载并解压 "sgmediation.zip" 后,需将解压得到的 "sgmediation" 文件移至 Stata 的 ado 目录结构中。ado(ado 目录并非“adolescent data organization”缩写,而是 Stata 的自定义命令存放目录)目录是 Stata 存放自定义命令的地方,应将文件放置于 "ado\base\s" 子目录下。这样,Stata 启动时会自动加载该目录下的所有 ado 文件,使 "sgmediation" 命令在 Stata 命令行中可用。 使用 sgmediation 插件的步骤如下:1. 安装插件:将解压后的 "sgmediation" 文件放入 Stata 的 ado 目录。如果 Stata 安装路径是 C:\Program Files\Stata\ado\base,则需将文件复制到 C:\Program Files\Stata\ado\base\s。2. 启动 Stata:打开 Stata,确保软件已更新至最新版本,以便识别新添加的 ado 文件。3. 加载插件:启动 Stata 后,在命令行输入 ado update sgmediation,以确保插件已加载并更新至最新版本。4
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值