Android-based annotation AOP framework

I. Introduction

Micro moment SpringBoot Services Framework Java back end of the fire, the cause can not do without the use of notes, the simple and easy way to configure annotation allows more applicable to the preparation of its community SpringBoot framework, which is gradually replacing the traditional annotation xml configuration . So notes in Android also been sublimated, the famous framework ButterKnife, Dagger2, Retrofit, and so on. Android today, bringing a more practical annotation framework AopArms , which are easy to use, which is commonly used in the preparation of the Android developers a set of annotations, such as logging, interception (login), asynchronous processing, caching, SP, delayed operation, regular tasks retry mechanism, try-catch safety mechanism, filtration frequent clicks, etc., there will be more follow-up more powerful annotations added.
Data in this chapter explains the basic content of the usage of Android, Android in practice on AOP, please refer to another article AOP development of Android programming .

Second, the introduction of way

1, adding a dependency in the main project

//引入aspectjx插件
apply plugin: 'android-aspectjx'

dependencies {
    ...
    implementation 'cn.com.superLei:aop-arms:1.0.1'
}

2, gradle script with the project directory added

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        //该库基于沪江aspect插件库
        classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.4'
    }
}

3, the initialization Application

AopArms.init(this);

Third, the basic use

1, the cache articles (of any type cacheable)

1、插入缓存
    @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;
    }
    
2、获取缓存
    private ArrayList<User> getUser() {
        return ACache.get(this).getAsList("userList", User.class);
    }

3、移除缓存
    @CacheEvict(key = "userList")
    public void removeUser() {
        Log.e(TAG, "removeUser: >>>>");
    }
    
3884117-b876b8f7981326d7
image

2, SharedPreferences articles (save target)

1、保存key到sp
    @Prefs(key = "article")
    private Article initArticle() {
        Article article = new Article();
        article.author = "jerry";
        article.title = "hello android";
        article.createDate = "2019-05-31";
        article.content = "this is a test demo";
        return article;
    }
    
2、从sp中移除key
    @PrefsEvict(key = "article")
    public void removeArticle() {
        Log.e(TAG, "removeArticle: >>>>");
    }

3, asynchronous articles

    @Async
    public void asyn() {
        Log.e(TAG, "useAync: "+Thread.currentThread().getName());
    }

4, try-catch security papers

    //自动帮你try-catch   允许你定义回调方法
    @Safe(callBack = "throwMethod")
    public void safe() {
        String str = null;
        str.toString();
    }
    
    //自定义回调方法(注意要和callBack的值保持一致)
    private void throwMethod(Throwable throwable){
        Log.e(TAG, "throwMethod: >>>>>"+throwable.toString());
    }

5, retry mechanism papers

     /**
     * @param count 重试次数
     * @param delay 每次重试的间隔
     * @param asyn 是否异步执行
     * @param retryCallback 自定义重试结果回调
     * @return 当前方法是否执行成功
     */
    @Retry(count = 3, delay = 1000, asyn = true, retryCallback = "retryCallback")
    public boolean retry() {
        Log.e(TAG, "retryDo: >>>>>>"+Thread.currentThread().getName());
        return false;
    }
    
    private void retryCallback(boolean result){
        Log.e(TAG, "retryCallback: >>>>"+result);
    }
3884117-8e368420904f4fae
image

6, regular tasks chapter

     /**
     * @param interval 初始化延迟
     * @param interval 时间间隔
     * @param timeUnit 时间单位
     * @param count 执行次数
     * @param taskExpiredCallback 定时任务到期回调
     */
    @Scheduled(interval = 1000L, count = 10, taskExpiredCallback = "taskExpiredCallback")
    public void scheduled() {
        Log.e(TAG, "scheduled: >>>>");
    }
    
    private void taskExpiredCallback(){
        Log.e(TAG, "taskExpiredCallback: >>>>");
    }
3884117-de566c1129847a93
image

7, the delay task papers

    //开启延迟任务(10s后执行该方法)
    @Delay(key = "test", delay = 10000L)
    public void delay() {
        Log.e(TAG, "delay: >>>>>");
    }
    
    //移除延迟任务
    @DelayAway(key = "test")
    public void cancelDelay() {
        Log.e(TAG, "cancelDelay: >>>>");
    }

8, filter frequently click

    //value默认500ms
    @SingleClick(value = 2000L)
    private void onclick(){
        Log.e(TAG, "onclick: >>>>");
    }

9, interception articles (such as login)

1、在需要进行拦截的方法添加注解
    @Intercept("login_intercept")
    public void loginIntercept() {
        Log.e(TAG, "intercept: 已登陆>>>>");
    }
2、(建议,统一处理)在Application中进行进行监听拦截回调
public class MyApplication extends Application {

    private static final String TAG = "MyApplication";
    private static MyApplication mApplication;
    
    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        AopArms.init(this);
        AopArms.setInterceptor(new Interceptor() {
            @Override
            public boolean intercept(String key, String methodName) throws Throwable {
                Log.e(TAG, "intercept methodName:>>>>>"+methodName);
                if ("login_intercept".equals(key)){
                    String userId = SPUtils.get(mApplication, "userId", "");
                    if (TextUtils.isEmpty(userId)){
                        Toast.makeText(mApplication, "您还没有登录", Toast.LENGTH_SHORT).show();
                        return true;//代表拦截
                    }
                }
                return false;//放行
            }
        });
    }
}

These are some basic common usage of the library, follow-up will add more annotations to simplify the development of Android, welcome to the issues to ask questions or make you think need more annotations demand.

GitHub Address: AopArms

Reproduced in: https: //www.jianshu.com/p/79bdcfa8f054

Guess you like

Origin blog.csdn.net/weixin_34399060/article/details/91114670
Recommended