美文网首页
初试AOP之AspectJ

初试AOP之AspectJ

作者: 欢乐的饲养员 | 来源:发表于2019-04-17 15:55 被阅读0次

一切的学习跟进步都来源于业务的需求
-----鲁迅

image

熟悉AOP以及AspectJ

引用另一篇非常不错的文章深入理解Android之AOP

android配置流程

  1. 在根目录下的gradle里配置aspectJ的gradle插件
dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        //gradle插件
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.5'
    }
  1. 在module/gradle中配置

apply plugin: 'android-aspectjx'

dependencies {
    api 'org.aspectj:aspectjrt:1.8.9'
}
  1. 配置完毕(感谢插件开发者 大大地降低了配置门槛)

aspectJ笔记

语法类

aspectj用饭简单例子

    @Before("execution(* android.support.v7.app.AppCompatActivity.onCreate(android.os.Bundle))")
    fun onCreatAspect(joinPoint: JoinPoint) {
        val key = joinPoint.signature.toString()
        val activity = joinPoint.target as Activity
        Toast.makeText(activity, "测试", Toast.LENGTH_SHORT).show()
        Log.e("LogManager", "初始化一下$key")
    }

@Before表示Advice,execution是JPoint 后面是(Signature)

关键字说明
JPoint 说明 用法
method call 函数被调用时插入调用前 call(methodSignature)
method execution 函数被执行时插入内部 execution(methodSignature)
constructor call 构造器被调用前插入 call(constrcutorSignature)
constructor execution 构造前被调用时插入内部 execution(constrcutorSignature)
field get/set 读/写变量 get/set(fieldSignature)
static initialization static 代码块初始化 staticinitialization(typeSignature)
handler 异常处理 handle(typeSignature)
advice execution advice执行 adviceexecution()

Pointcut 中的 Signature 参考:

image

以上的 Signature 都是由一段表达式组成,且每个关键词之间都有“空格”,下面是对关键词的解释:


Advice 相关语法介绍

Advice 说明
@Before(pointcut) 执行JPoint之前
@After(pointcut) 执行JPoint之后
@Around(Pointcut) 替换原来的代码,如果需要执行原来的,调用ProceedingJoinPoint#proceed() 不能跟上面两个一起使用
用法

用法格式都是@Advice(JPoint(Signature ))

JoinPoint 类解析

JoinPoint类是整个aop过程中唯一的参数
要想获取其他的参数,则需要通过JoinPoint的api

JoinPoint api

方法名 功能
Signature getSignature(); 获取封装了署名信息的对象,在该对象中可以获取到目标方法名,所属类的Class等信息
Object[] getArgs(); 获取传入目标方法的参数对象
Object getTarget(); 获取被代理的对象
Object getThis(); 获取代理对象

相关文章

  • 初试AOP之AspectJ

    一切的学习跟进步都来源于业务的需求-----鲁迅 熟悉AOP以及AspectJ 引用另一篇非常不错的文章深入理解A...

  • AOP之AspectJ - 代码注入

    AOP之AspectJ - 代码注入 [TOC] 一、AOP简介 1.1 什么是AOP编程 AOP是Aspect ...

  • 基于AspectJ的注解AOP开发

    基于AspectJ的注解AOP开发 AspectJ开发准备 导入相关jar包 引入aop约束 @AspectJ通知...

  • Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ ...

  • Spring基于AspectJ的AOP的开发

    AspectJ实现AOP,实现方式: ——注解方式 ——XML方式 AspectJ实现AOP的方式比传统的AOP方...

  • 2020-08-28:Spring AOP和AspectJ AO

    前言 每日一题专栏 Spring AOP和AspectJ AOP有什么区别? AspectJ和Spring AOP...

  • Aop之AspectJ

    AOP之AspectJ 前言:这几天一直在学习aop切面编程,以前一直也有听过aop但是实际用的还是比较少,不是很...

  • AOP之AspectJ

    aop实现的三大方式(反射 (xutil) apt注解(ButterKnife) aspect (本文即将讲到的...

  • 29、Spring5之AOP操作-准备工作

    AOP操作(准备) 1、Spring框架一般都是基于AspectJ实现AOP操作。 什么是AspectJ Aspe...

  • 02 AOP切入点详解

    Spring AOP支持的AspectJ切入点指示符 Spring AOP支持的AspectJ切入点指示符如下: ...

网友评论

      本文标题:初试AOP之AspectJ

      本文链接:https://www.haomeiwen.com/subject/edunwqtx.html