自定义LogUtil注解

Log打印

package com.example.framework.manager;

import android.util.Log;

import com.example.framework.BuildConfig;

public class LogUtil {

    private static String className;
    private static String methodName;
    private static int lineName;
    private static final String TAG = "Shopmall18809";

    public static void v(String message) {
        if (BuildConfig.LOG_DEBUG) {
            createLogName(new Throwable().getStackTrace());
            Log.v(TAG, className + " " + methodName + " " + lineName);
        }
    }


    public static void d(String message) {
        if (BuildConfig.LOG_DEBUG) {
            createLogName(new Throwable().getStackTrace());
            Log.d(TAG, "" + className + " " + methodName + " " + lineName + " " + message);
        }
    }
    public static void i(String message) {
        if (BuildConfig.LOG_DEBUG) {
            createLogName(new Throwable().getStackTrace());
            Log.i(TAG, className + " " + methodName + " " + lineName);
        }
    }
    public static void e(String message) {
        if (BuildConfig.LOG_ERROR) {
            createLogName(new Throwable().getStackTrace());
            Log.e(TAG, className + " " + methodName + " " + lineName);
        }
    }

    private static void createLogName(StackTraceElement[] stackTraceElements) {
        className = stackTraceElements[1].getClassName();//获取调用该log的类名,里面含有包名
        methodName = stackTraceElements[1].getMethodName();//获取调用该log的方法名
        lineName = stackTraceElements[1].getLineNumber();//获取调用该log的行数值
    }

}

build.gradle

apply plugin: 'com.android.library'
apply plugin: 'org.greenrobot.greendao' // 1.apply plugin
android {

    compileSdkVersion 30
    buildToolsVersion "30.0.2"

    sourceSets {
        main.jniLibs.srcDirs = ['libs']
    }
    defaultConfig {
        minSdkVersion 22
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'
    }

    buildTypes {

        debug{
            buildConfigField("boolean","LOG_DEBUG","false");
            buildConfigField("boolean","LOG_ERROR","false");

            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'

        }

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }


    greendao {
        // 指定数据库schema版本号,迁移等操作会用到
        schemaVersion 1
        // 设置生成数据库文件的目录,默认是在build中,可以将生成的文件放到我们的java目录中
        targetGenDir 'src/main/java'
        // 设置生成的数据库相关文件的包名,默认为entity所在的包名
        daoPackage 'com.example.framework.greendao'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.jakewharton:disklrucache:2.0.2'
    api 'com.blankj:utilcodex:1.26.0'
    implementation 'com.android.support:recyclerview-v7:30.0.0'//recyclerview
    implementation project(path: ':commened')
    implementation project(path: ':net')
    api 'org.greenrobot:greendao:3.3.0' // 2.add library
    //工具类
    api 'com.blankj:utilcodex:1.26.0'

}

private void loadRoute(Class<? extends java.lang.annotation.Annotation> annotation) { // 获取所有被指定注解修饰的Bean Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(annotation); beansWithAnnotation.forEach((beanName, bean) -> { try { Class<?> beanClass = bean.getClass(); Annotation targetAnnotation = beanClass.getAnnotation(annotation); Method route = targetAnnotation.annotationType().getMethod(ROUTE_KEY); String routeValue = (String) route.invoke(targetAnnotation); Method routeDescribe = targetAnnotation.annotationType().getMethod(DESCRIBE_KEY); String describeValue = (String) routeDescribe.invoke(targetAnnotation); // 获取类中被指定注解修饰的方法 Method[] methods = beanClass.getDeclaredMethods(); Arrays.stream(methods) .forEach(method -> { if(routes.containsKey(routeValue+"/"+method.getName())){ throw new RouteLoadException("加载路由失败,路由地址重复: " + routeValue+"/"+method.getName()); } routes.put(routeValue+"/"+method.getName(),beanClass.getName()+"_"+method.getName()); SysRoute sysRouteMethod = new SysRoute(); sysRouteMethod.setControllerEnName(beanName); sysRouteMethod.setControllerDescribe(describeValue); sysRouteMethod.setApiName(method.getName()); ApiDescribe apiDescribe = method.getAnnotation(ApiDescribe.class); if (Objects.isNull(apiDescribe)){ throw new RouteLoadException("加载路由失败: " + bean.getClass().getName()+"_"+method.getName() + "未配置@ApiDescribe注解"); } sysRouteMethod.setApiDescribe(apiDescribe.describe()); SYS_ROUTES_PERMISSION.add(sysRouteMethod); }); }catch (Exception e){ throw new RouteLoadException("加载路由失败: " + bean.getClass().getName(), e); } }); LogUtil.getLogger().info("加载接口数量:{}个", routes.size()); //LogUtil.getLogger().info("加载接口数量:{}", JSONArray.toJSONString(SYS_ROUTES_PERMISSION)); /*logger.info("------------------------加载接口start------------------------"); for (Map.Entry<String, String> route : routes.entrySet()){ logger.info("接口地址:{} -----------> 对应类及方法:{}", route.getKey(),route.getValue()); } logger.info("------------------------加载接口end------------------------");*/ } 这是一个在springbooot项目启动后加载接口路径的方法,现在是把扫描到的接口全都放在了map里面管理,如何实现把扫描到的接口托管给spring容器进行管理
最新发布
09-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值