AOP面向切面编程实现
1:配置
-
1.1:引用 aspectjrt.jar 库
//添加lib工程引用
compile files('libs/aspectjrt.jar')
-
1.2:指定使用该编译器,即.java文件转.class文件的过程
//在这一行下面加
//apply plugin: 'com.android.application'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.aspectj:aspectjtools:1.8.8'
classpath 'org.aspectj:aspectjweaver:1.8.8'
}
}
-
1.3: log日志,方便调试
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
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.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;
}
}
}
}
2:开发注解,用于哪些地方需要加入面向切面的逻辑
/** 用来标识性能监测*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface BehaviorTrace {
String value();
}
3:开发切面类
3.1:创建切面类,并添加注解 @Aspect
//该注解用于生成切面
@Aspect
public class BehaviorTraceAspect {
}
3.2:定义切面的规则
//定义切入点(此语法不能错)
@Pointcut("execution(@com.example.a48608.dn_aop_20180305.annotation.BehaviorTrace * *(..))")
//方法名可自定义
public void methodAnnotatedWithBehaviorTrace(){
}
3.3:处理进入切面的内容
//方法名可自定义
//其他为固定写法,返回值,参数,抛异常
//@Before() 在切入点之前运行
//@After() 在切入点之后运行
//@Around() 在切入点前后都运行
///@Around() 括号内引入上一步的切入点
@Around("methodAnnotatedWithBehaviorTrace()")
public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable{
MethodSignature methodSignature=(MethodSignature)joinPoint.getSignature();
String className=methodSignature.getDeclaringType().getSimpleName();
String methodName=methodSignature.getName();
String funName=methodSignature.getMethod().getAnnotation(BehaviorTrace.class).value();
//统计时间
long begin=System.currentTimeMillis();
//执行原来的逻辑
Object result=joinPoint.proceed();
long duration=System.currentTimeMillis()-begin;
Log.d("jett",String.format("功能:%s,%s类的%s方法执行了,用时%d ms",funName,className,methodName,duration));
return result;
}