AOP 切面思想 AspectJ实现

AOP切面设计

场景举例

1:判断是否登录

正常编写代码

	/**
     * 跳转到我的页面
     */
    public void toMyAttention() {
        // 判断当前用户是否登录
        if(LoginHelper.isLogin(this)) {
            // 如果登录才跳转,进入我的页面
            Intent intent = new Intent(this, WaitReceivingActivity.class);
            startActivity(intent);
        }else{
          //跳转到登录页面,先登录
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        }
    }

AOP方案:

/**
    *  跳转到我的页面
    */
    @CheckLogin
    public void toMyAttention() {
         Intent intent = new Intent(this, WaitReceivingActivity.class);
         startActivity(intent);
    }
2:防止view连续点击
public class SingleClickUtils {
  private final static int SPACE_TIME = 500;//2次点击的间隔时间,单位ms
  private static long lastClickTime;

  public synchronized static boolean isSingleClick() {
    long currentTime = System.currentTimeMillis();
    boolean isClick;
    if (currentTime - lastClickTime > SPACE_TIME) {
      isClick = true;
    } else {
      isClick = false;
    }
    lastClickTime = currentTime;
    return isClick;
  }
}
        findViewById(R.id.check_login_btn).setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                //单击
                if (SingleClickUtils.isSingleClick()) {
                    
                }
            }
        });

AOP方案:

//切面类
@Aspect
public class AspectTest{
    @Around("execution(* android.view.View.OnClickListener.onClick(..))")
    public void onClickLitener(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Log.e(TAG, "OnClick");
        if (SingleClickUtils.isSingleClick()) {
            proceedingJoinPoint.proceed();
        }
    }
   }
        findViewById(R.id.check_login_btn).setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {

            }
        });
其他场景

日志 数据校验 统计 权限验证

什么是AOP

AOP是Aspect Oriented Programming的缩写,即『面向切面编程』。它和我们平时接触到的OOP都是编程的不同思想,OOP,即『面向对象编程』,它提倡的是将功能模块化,对象化,而AOP的思想,则不太一样,它提倡的是针对同一类问题的统一处理,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

AspectJ

AOP是一种思想, AspectJ是一种实现方案, 可以说是对java的扩展. AspectJ是一种语言, 可以使用AspectJ语法也可以使用注解

基本概念

Aspect 切面:切面是切入点和通知的集合。

PointCut 切入点:切入点是指那些通过使用一些特定的表达式过滤出来的想要切入Advice的连接点。

Advice 通知:通知是向切点中注入的代码实现方法。

Weaving 编织:主要是在编译期使用AJC将切面的代码注入到目标中, 并生成出代码混合过的.class的过程.

实践步骤

1:build.gradle配置
apply plugin: 'com.android.application'
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.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;
            }
        }
    }
}

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.aopdemo"
        minSdkVersion 17
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'org.aspectj:aspectjrt:1.8.9'
    testCompile 'junit:junit:4.12'
}

因为AspectJ是对java的扩展,而且是完全兼容java的。但是编译时得用Aspect专门的编译器,这里的配置就是使用Aspect的编译器,单独加入aspectj依赖是不行的。

2:创建切面类

用来处理具体逻辑

@Aspect
public class CheckLoginAspectJ {
    private static final String TAG = "CheckLogin";

    /**
     * 找到处理的切点
     * * *(..)  可以处理CheckLogin这个类所有的方法
     */
    @Pointcut("execution(@com.example.aopdemo.login.CheckLogin  * *(..))")
    public void executionCheckLogin() {
    }

    /**
     * 处理切面
     *
     * @param joinPoint
     * @return
     */
    @Around("executionCheckLogin()")
    public Object checkLogin(ProceedingJoinPoint joinPoint) throws Throwable {
        Log.i(TAG, "checkLogin: ");
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        CheckLogin checkLogin = signature.getMethod().getAnnotation(CheckLogin.class);
        if (checkLogin != null) {
            Context context = (Context) joinPoint.getThis();
            if (MyApplication.isLogin) {
                Log.i(TAG, "checkLogin: 登录成功 ");
                return joinPoint.proceed();
            } else {
                Log.i(TAG, "checkLogin: 请登录");
                Toast.makeText(context, "请登录", Toast.LENGTH_SHORT).show();
                return null;
            }
        }
        return joinPoint.proceed();
    }
}

在Pointcut这里,execution也就是以方法执行时为切点,触发Aspect类。而execution里面的字符串是触发条件,也是具体的切点。我来解释一下参数的构成。“execution(@com.example.aopdemo.login.CheckLogin * *(…))”这个条件是所有加了CheckLogin注解的方法或属性都会是切点,范围比较广。

**:表示是任意包名
…:表示任意类型任意多个参数
+: 表示任意子类
PointCut 切入点:
在这里插入图片描述
在这里插入图片描述

Advice 通知
在这里插入图片描述
创建完Aspect类之后,还需要一个注解类,它的作用是:哪里需要做切点,那么哪里就用注解标注一下,这样方便快捷。

3:创建注解类
@Target(ElementType.METHOD) //可以注解在方法 上
@Retention(RetentionPolicy.RUNTIME) //运行时(执行时)存在
public @interface CheckLogin {
}
4:使用注解
@Target(ElementType.METHOD) //可以注解在方法 上
@Retention(RetentionPolicy.RUNTIME) //运行时(执行时)存在
public @interface CheckLogin {
}

触发流程:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值