Android开发之AOP编程

1、简介:

AOP(Aspect-Oriented Programming,面向切面编程),可谓是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。AOP技术利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其名为“Aspect”,即方面。所谓“方面”,简单地说,就是将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减小系统的重复代码,下降 模块间的耦合度,并有利于将来的可操做性和可维护性。Android相关AOP经常使用的方法有 JNI HOOK 和 静态织入,本文以静态织入的方式之一AspectJ来进行讲解使用。(在编译期织入,切面直接以字节码的形式编译到目标字节码文件中,这要求使用特殊的 Java 编译器。)android

2、使用场景

对于Java后端来讲最多见的场景应该就是日志记录、检查用户权限、参数校验、事务处理、缓存等等了,而对于Android来讲也是比较普遍的(适用与Java的一样都适用于Android),例如常见的有记录日志、检查权限申请、判断用户登陆状态、检查网络状态、过滤重复点击等等,能够根据项目实际须要的一些业务场景来自定义你所须要的。git

3、实践

一、添加依赖
apply plugin: 'android-aspectjx'

dependencies {
    ...
    compile 'org.aspectj:aspectjrt:1.8.9'
}
复制代码

项目跟目录的gradle脚本中加入github

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        //此处推荐该库,由沪江出品,可免去配置各类复杂任务
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4'
    }
}
复制代码
二、AOP注解与使用
@Aspect:声明切面,标记类
@Pointcut(切点表达式):定义切点,标记方法
@Before(切点表达式):前置通知,切点以前执行
@Around(切点表达式):环绕通知,切点先后执行
@After(切点表达式):后置通知,切点以后执行
@AfterReturning(切点表达式):返回通知,切点方法返回结果以后执行
@AfterThrowing(切点表达式):异常通知,切点抛出异常时执行
复制代码

切点表达式的示例:编程

如:execution(@com.xxx.aop.TimeLog *.(..))
切点表达式的组成:
execution(@注解 访问权限 返回值的类型 包名.函数名(参数))
复制代码

下面简单列举两个简单例子:json

@Before("execution(@com.xxx.aop.activity * *(..))")
public void before(JoinPoint point) {
    Log.i(TAG, "method excute before...");
}
匹配activity包下的全部方法,在方法执行前输出日志
复制代码
@Around("execution(@cn.com.xxx.Async * *(..))")
public void doAsyncMethod(ProceedingJoinPoint joinPoint) {
    Log.i(TAG, "method excute before...");
    joinPoint.proceed();
    Log.i(TAG, "method excute after...");
}
匹配带有Async注解的方法,在方法执行先后分别输出日志
复制代码
@Around("execution(@cn.com.xxx.Async * *(..)) && @annotation(async)")
public void doAsyncMethod(ProceedingJoinPoint joinPoint, Async async) {
    Log.i(TAG, "value>>>>>"+async.value());
    <!--to do somethings-->
    joinPoint.proceed();
    <!--to do somethings-->
}
//注意:@annotation(xxx)必须与下面的的参数值对应
匹配带有Async注解的方法,并获取注解中的值,去作相应处理。
复制代码
三、实际应用举例
场景1、利用注解实现缓存处理

一、定义注解Cache以下:后端

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Cache {

    String key(); //缓存的key

    int expiry() default -1; // 过时时间,单位是秒
}
复制代码

二、编写Aspect实现缓存

@Aspect
public class CacheAspect {
    private static final String POINTCUT_METHOD = "execution(@cn.com.xxx.annotation.Cache * *(..))";

    @Pointcut(POINTCUT_METHOD)
    public void onCacheMethod() {
    }

    @Around("onCacheMethod() && @annotation(cache)")
    public Object doCacheMethod(ProceedingJoinPoint joinPoint, Cache cache) throws Throwable {
        //获取注解中的key
        String key = cache.key();
        //获取注解中的过时时间
        int expiry = cache.expiry();
        //执行当前注解的方法(放行)
        Object result = joinPoint.proceed();
        //方法执行后进行缓存(缓存对象必须是方法返回值)
        ACache aCache = ACache.get(AopArms.getContext());
        if (expiry>0) {
            aCache.put(key,(Serializable)result,expiry);
        } else {
            aCache.put(key,(Serializable)result);
        }
        return result;
    }
}
复制代码

此处引用ACache该项目中的缓存实现,仅一个Acache文件,我的以为仍是比较好用的,固然你也能够本身去实现。 三、测试安全

public static void main(String[] args) {
        initData();
        getUser();
    }
    
    //缓存数据
    @Cache(key = "userList")
    private ArrayList<User> initData() {
        ArrayList<User> list = new ArrayList<>();
        for (int i=0; i<5; i++){
            User user = new User();
            user.setName("艾神一不当心:"+i);
            user.setPassword("密码:"+i);
            list.add(user);
        }
        return list;
    }
    
    //获取缓存
    private void getUser() {
        ArrayList<User> users = ACache.get(this).getAsList("userList", User.class);
        Log.e(TAG, "getUser: "+users);
    }
    
复制代码
场景2、利用注解实现缓存移除

一、定义注解CacheEvict以下:bash

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CacheEvict {

    //须要移除的key
    String key();

    // 缓存的清除是否在方法以前执行, 默认表明缓存清除操做是在方法执行以后执行;若是出现异常缓存就不会清除
    boolean beforeInvocation() default false;
    
    //是否清空全部缓存
    boolean allEntries() default false;
}
复制代码

二、编写Aspect实现网络

@Aspect
public class CacheEvictAspect {
    private static final String POINTCUT_METHOD = "execution(@cn.com.xxx.annotation.CacheEvict * *(..))";

    //切点位置,全部的CacheEvict处
    @Pointcut(POINTCUT_METHOD)
    public void onCacheEvictMethod() {
    }
    
    //环绕处理,并拿到CacheEvict注解值
    @Around("onCacheEvictMethod() && @annotation(cacheEvict)")
    public Object doCacheEvictMethod(ProceedingJoinPoint joinPoint, CacheEvict cacheEvict) throws Throwable {
        String key = cacheEvict.key();
        boolean beforeInvocation = cacheEvict.beforeInvocation();
        boolean allEntries = cacheEvict.allEntries();
        ACache aCache = ACache.get(AopArms.getContext());
        Object result = null;
        if (allEntries){
            //若是是所有清空,则key不须要有值
            if (!TextUtils.isEmpty(key))
                throw new IllegalArgumentException("Key cannot have value when cleaning all caches");
            aCache.clear();
        }
        if (beforeInvocation){
            //方法执行前,移除缓存
            aCache.remove(key);
            result = joinPoint.proceed();
        }else {
            //方法执行后,移除缓存,若是出现异常缓存就不会清除(推荐)
            result = joinPoint.proceed();
            aCache.remove(key);
        }
        return result;
    }
}
复制代码

三、测试

public static void main(String[] args) {
        removeUser();
        getUser();
    }
    
    //移除缓存数据
    @CacheEvict(key = "userList")
    private void removeUser() {
        Log.e(TAG, "removeUser: >>>>");
    }
    
    //获取缓存
    private void getUser() {
        ArrayList<User> users = ACache.get(this).getAsList("userList", User.class);
        Log.e(TAG, "getUser: "+users);
    }
复制代码

能够看到执行结果:

最后推荐本人写的一个基于AOP的注解框架AopArms,用法及其简单,参考一款基于AOP的Android注解框架,里面编写了Android开发中经常使用的一套注解,如日志、异步处理、缓存、SP、延迟操做、定时任务、重试机制、try-catch安全机制、过滤频繁点击等,后续还会有更多更强大的注解功能加入,欢迎star。

相关文章
相关标签/搜索