理解android aop

2018-09-13  本文已影响0人  黑领航

学习博客地址

https://www.jianshu.com/p/f4d8511461a3

https://www.cnblogs.com/weizhxa/p/8567942.html

https://blog.csdn.net/sw5131899/article/details/53885957

一 首先配置aop

需要添加一下

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.8.9'

        classpath 'org.aspectj:aspectjweaver:1.8.9'

    }

}

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.8",

"-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.errormessage.message,message.thrown

break;

case IMessage.WARNING:

log.warnmessage.message,message.thrown

break;

case IMessage.INFO:

log.info message.message,message.thrown

break;

case IMessage.DEBUG:

log.debug message.message,message.thrown

break;

}

}

}

}

implementation 'org.aspectj:aspectjrt:1.8.1'     // 在dependencies配置

二 ,使用

三 aop注解

@Aspect:声明切面,标记类 

@Pointcut(切点表达式):定义切点,标记方法 

@Before(切点表达式):前置通知,切点之前执行 

@Around(切点表达式):环绕通知,切点前后执行 

@After(切点表达式):后置通知,切点之后执行 

@AfterReturning(切点表达式):返回通知,切点方法返回结果之后执行

 @AfterThrowing(切点表达式):异常通知,切点抛出异常时执行

上一篇下一篇

猜你喜欢

热点阅读