美文网首页
AOP之Aspectj 使用

AOP之Aspectj 使用

作者: 猫KK | 来源:发表于2018-11-22 21:17 被阅读0次

关于Aspectij 在android 中的使用

在 app目录下的bulid.gradle 文件中配置如下信息:

dependencies {
    ....
    //这样子不需要在lib 文件夹中引入jar 包
    compile 'org.aspectj:aspectjrt:1.9.2'
    //这种需要在lib 文件中放入jar包
    //compile file(libs/aspectjrt.jar)
}
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.aspectj:aspectjtools:1.9.2'
        classpath 'org.aspectj:aspectjweaver:1.9.2'
    }
}

repositories {
    mavenCentral()
}
final def log = project.logger
final def variants = project.android.applicationVariants

variants.all { variant ->
    if (!variant.buildType.isDebuggable()) {
        log.debug("Skipping non-debuggable build type '${variant.buildType.name}'.")
        return
    }

    JavaCompile javaCompile = variant.javaCompile
    javaCompile.doLast {
        String[] args = ["-showWeaveInfo",
                         "-1.9",
                         "-inpath", javaCompile.destinationDir.toString(),
                         "-aspectpath", javaCompile.classpath.asPath,
                         "-d", javaCompile.destinationDir.toString(),
                         "-classpath", javaCompile.classpath.asPath,
                         "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)]
        log.debug "ajc args: " + Arrays.toString(args)

        MessageHandler handler = new MessageHandler(true)
        new Main().run(args, handler)
        for (IMessage message : handler.getMessages(null, true)) {
            switch (message.getKind()) {
                case IMessage.ABORT:
                case IMessage.ERROR:
                case IMessage.FAIL:
                    log.error message.message, message.thrown
                    break
                case IMessage.WARNING:
                    log.warn message.message, message.thrown
                    break
                case IMessage.INFO:
                    log.info message.message, message.thrown
                    break
                case IMessage.DEBUG:
                    log.debug message.message, message.thrown
                    break
            }
        }
    }

如上配置完成,下面编写代码
新建注解类

 //表示方法上的注解
@Target(ElementType.METHOD)
//运行时注解
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckPerformance {
}

新建需要自定义行为的类

//注意需要加上 Aspect 注解
@Aspect
public class MethodBehaviorAspect {
    //新建方法 增加 Pointcut 注解 后面对应注解类的包名
    @Pointcut("execution(@com.lsghelper.Aspectj.CheckPerformance * *(..))")
    public void firstMethodAnnotationBehavior() {

    }

    //新建方法 增加 Around 注解 处理你想要的逻辑
    @Around("firstMethodAnnotationBehavior()")
    public Object CheckPerformance(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        //类名
        String simpleName = methodSignature.getDeclaringType().getSimpleName();
        //方法名
        String name = methodSignature.getName();
        CheckPerformance checkPerformance = methodSignature.getMethod().getAnnotation(CheckPerformance.class);
        long start = System.currentTimeMillis();
        //注意,调用这一句之后代表你注解的方法已经执行完成
        Object proceed = joinPoint.proceed();
        if (checkPerformance != null) {
            long end = System.currentTimeMillis();
            Log.e("TAG", "耗时: ---->" + (end - start));
        }
        return proceed;
    }
}

之后在对应的方法中使用注解

    @CheckPerformance
    public void hookHanlder() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
}

以上就是Aspectj 在android 中的使用方法。
至于其原理,和 ButterKnife 基本一致,主要在编译时修改增加了注解的对应的方法,自己拼接上我们写的逻辑,具体可以反编译 apk 查看可知。

相关文章

  • AspectJ的AOP开发

    1.使用AspectJ 实现AOP • AspectJ是一个基于Java语言的AOP框架 • Spring2.0以...

  • Spring--AOP使用

    Spring的AOP也是基于AspectJ,和安卓中(之前文章:Android--AOP架构设计之使用Aspect...

  • AOP之AspectJ - 代码注入

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

  • AOP之Aspectj 使用

    关于Aspectij 在android 中的使用 在 app目录下的bulid.gradle 文件中配置如下信息:...

  • spring-aop

    aop概念aop概念aop术语AOP实现方式1、spring-aop(使用xml文件实现AOP)2、AspectJ...

  • 基于AspectJ的注解AOP开发

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

  • Spring4-2-AOP配置

    一.AOP概念 二.AOP术语 三.Spring AOP框架AspectJ配置使用(基于注解的方式) (1)必要的...

  • 第12章-Spring基于注解配置AOP

    Spring 的 AOP 功能是基于 AspectJ 实现的,支持使用注解声明式定义 AOP 切面。 理解 AOP...

  • Spring 基于 AspectJ 的 AOP 开发

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

  • Spring之使用注解配置Spring AOP

    Spring框架通过注解配置AOP是基于AspectJ实现的。 Spring框架只是直接使用了AspectJ的注解...

网友评论

      本文标题:AOP之Aspectj 使用

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