ARouter(二)源码解析

2023-11-19

前言

这一篇我们来具体看一下ARouter的实现原理,如果你之前没有接触过ARouter,可以先阅读上一篇:
Android:从零开始打造自己的深度链接库(一):ARouter简介
废话不多,我们赶紧分析源码。

正文

首先我们从github下载最新的源码:

被选中的三个Module是我们今天分析的重点:

arouter-annotation

从名称看我们可以猜到这是自定义注解的库,我们就直接截个图:

arouter-annotation

我们看到了之前使用过的@Router,@Autowired,@Interceptor,这个库就直接略过。

arouter-compiler

我们先思考一个问题:

为什么通过注解,我们就可以跳转到指定的页面呢,ARouter是怎么做到的?

如果你之前看过EventBus,或者LitePal的源码,这一篇对于你来说就是小case。因为核心的原理都是一样的。

@AutoService(Processor.class)
@SupportedOptions(KEY_MODULE_NAME)
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedAnnotationTypes({ANNOTATION_TYPE_AUTOWIRED})
public class AutowiredProcessor extends AbstractProcessor 

要想编译时处理注解,需要实现对应的AbstractProcessor,并使用@AutoService(Processor.class)注解,这样在编译时,这个注解解析器就会被执行,我们可以在编译时生成源文件来保存必要的信息,这样做的好处是,把注解解析的成本放在编译期间,从而节省了运行时的时间,对于用户来说,这样的体验是值得推荐的。

我们以AutowiredProcessor为例,分析一下编译期间ARouter到底做了哪些工作:

@Override
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) {
        if (CollectionUtils.isNotEmpty(set)) {
            try {
                logger.info(">>> Found autowired field, start... <<<");
                // 取出Autowired.class
                categories(roundEnvironment.getElementsAnnotatedWith(Autowired.class));
                // 生成帮助文件
                generateHelper();

            } catch (Exception e) {
                logger.error(e);
            }
            // 注解处理完毕返回true
            return true;
        }

        return false;
    }

直接的解析工作需要重写process方法,从所有的注解中取出需要解析的注解集合,先看看这个categories()方法做了哪些处理:

private void categories(Set<? extends Element> elements) throws IllegalAccessException {
		// 判断使用了Autowired注解是否为空
        if (CollectionUtils.isNotEmpty(elements)) {
            for (Element element : elements) {
            	// 得到被注解的元素的父级元素
            	// 例如被注解的是一个属性,那么得到的就是这个类
                TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
				// 被注解的元素不可以被private修饰
                if (element.getModifiers().contains(Modifier.PRIVATE)) {
                    throw new IllegalAccessException("The inject fields CAN NOT BE 'private'!!! please check field ["
                            + element.getSimpleName() + "] in class [" + enclosingElement.getQualifiedName() + "]");
                }
				// 因为编译解析器,可能被调用多次,所以这里使用一个键值对组合保存了之前解析过的信息,提高执行效率
				// 如果已经解析过了,找到之前的key,直接添加到集合中
                if (parentAndChild.containsKey(enclosingElement)) { // Has categries
                    parentAndChild.get(enclosingElement).add(element);
                }
                // 没有解析过,保存到集合中 
				else {
                    List<Element> childs = new ArrayList<>();
                    childs.add(element);
                    parentAndChild.put(enclosingElement, childs);
                }
            }

            logger.info("categories finished.");
        }
    }

上面的代码注释已经写的很详细了,这里我们了解到了新的信息:

@Autowired不能修饰private属性!!!

接下来我们分析一下generateHelper()方法,因为代码比较多,所以我把几个核心的地方单独分析,刚才我们已经把每一个元素和对应的@AutoWired注解都保存了起来,那肯定是要遍历就这个集合了:

for (Map.Entry<TypeElement, List<Element>> entry : parentAndChild.entrySet()) {
                // 创建inject方法
                MethodSpec.Builder injectMethodBuilder = MethodSpec.methodBuilder(METHOD_INJECT)
                        .addAnnotation(Override.class)
                        .addModifiers(PUBLIC)
                        .addParameter(objectParamSpec);
                // 使用了注解的类
                TypeElement parent = entry.getKey();
                // 被注解修饰的元素
                List<Element> childs = entry.getValue();

                // 类名
                String qualifiedName = parent.getQualifiedName().toString();
                // 包名
                String packageName = qualifiedName.substring(0, qualifiedName.lastIndexOf("."));
                // 要生成的源文件的名称
                String fileName = parent.getSimpleName() + NAME_OF_AUTOWIRED;

                // 对源文件添加public修饰符,添加要实现的接口,和注释
                TypeSpec.Builder helper = TypeSpec.classBuilder(fileName)
                        .addJavadoc(WARNING_TIPS)
                        .addSuperinterface(ClassName.get(type_ISyringe))
                        .addModifiers(PUBLIC);

                // 添加一个私有的serializationService属性
                FieldSpec jsonServiceField = FieldSpec.builder(TypeName.get(type_JsonService.asType()), "serializationService", Modifier.PRIVATE).build();
                helper.addField(jsonServiceField);
                // 为刚才创建的inject方法添加代码
                // serializationService = ARouter.getInstance().navigation(SerializationService.class)
                injectMethodBuilder.addStatement("serializationService = $T.getInstance().navigation($T.class)", ARouterClass, ClassName.get(type_JsonService));
                // ClassName.get(parent)类名
                injectMethodBuilder.addStatement("$T substitute = ($T)target", ClassName.get(parent), ClassName.get(parent));
				// 遍历被注解修饰的元素列表
				// 此处先省略
                 for (Element element : childs) {
                	 ...
                }
                // 源文件添加方法
                helper.addMethod(injectMethodBuilder.build());

                // 生成源文件
                JavaFile.builder(packageName, helper.build()).build().writeTo(mFiler);
            }

果然这里要开始生成源文件了,首先定义了要生成的文件的名称和位置(包名),然后创建一个public的inject方法,此处添加了一个SerializationService服务,这个服务是序列化的服务,我们可以先忽略。

然后就压开始遍历被@Autowired注解的元素集合了:

Autowired fieldConfig = element.getAnnotation(Autowired.class);
// 被注解的属性的名称
String fieldName = element.getSimpleName().toString();
// 如果这是一个IProvide的子类
if (types.isSubtype(element.asType(), iProvider)) { 
	// 如果在注解中特别指定了名称
	if ("".equals(fieldConfig.name())) {    // User has not set service path, then use byType.
		// 在inject方法中使用属性名,获取属性对应的实例
   		injectMethodBuilder.addStatement(
              "substitute." + fieldName + " = $T.getInstance().navigation($T.class)",
              ARouterClass,
              ClassName.get(element.asType())
         );
      } else {   
           // 使用注解中的名称找到对应的实例
          injectMethodBuilder.addStatement(
                "substitute." + fieldName + " = ($T)$T.getInstance().build($S).navigation()",
                          ClassName.get(element.asType()),
                           ARouterClass,
                           fieldConfig.name()
           );
       }

       // 判断该属性是否是必须,如果没有找到可以实例化的对象,注解抛出错误
       if (fieldConfig.required()) {
             injectMethodBuilder.beginControlFlow("if (substitute." + fieldName + " == null)");
             injectMethodBuilder.addStatement(
                                    "throw new RuntimeException(\"The field '" + fieldName + "' is null, in class '\" + $T.class.getName() + \"!\")", ClassName.get(parent));
              injectMethodBuilder.endControlFlow();
        }
}
 else{
	...
}

首先判断被@Autowired注解的属性是否是一个IProvider接口,IProvider接口表示一个对外开放的服务,不同Module可以通过IProvider得到对应的服务,这样不同Module之间就可以进行通信。

String originalValue = "substitute." + fieldName;
String statement = "substitute." + fieldName + " = " + buildCastCode(element) + "substitute.";
boolean isActivity = false;
// 是否是Activity的子类
if (types.isSubtype(parent.asType(), activityTm)) {  // Activity, then use getIntent()
       isActivity = true;
       // 可以看Activity要通过intent进行属性的初始化
       statement += "getIntent().";
} 
// 是否是Frament的子类
else if (types.isSubtype(parent.asType(), fragmentTm) || types.isSubtype(parent.asType(), fragmentTmV4)) {   // Fragment, then use getArguments()
        // 可以看Activity要通过getArguments进行属性的初始化
        statement += "getArguments().";
} 
// 从此判断可以看出,@Autowired注解的类型只有IProvder,Activity和Fragment
else {
          throw new IllegalAccessException("The field [" + fieldName + "] need autowired from intent, its parent must be activity or fragment!");
}
                        
// 根据对应的类型,找到需要的类型
// 例如getInt(),getBoolean()
statement = buildStatement(originalValue, statement, typeUtils.typeExchange(element), isActivity);
// 如果是一个对象,我们要对应serializationService来实现初始化
if (statement.startsWith("serializationService.")) {   // Not mortals
      injectMethodBuilder.beginControlFlow("if (null != serializationService)");
      injectMethodBuilder.addStatement(
                 "substitute." + fieldName + " = " + statement,
                  (StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name()),
                                    ClassName.get(element.asType())
       );
      injectMethodBuilder.nextControlFlow("else");
      injectMethodBuilder.addStatement(
               "$T.e(\"" + Consts.TAG + "\", \"You want automatic inject the field '" + fieldName + "' in class '$T' , then you should implement 'SerializationService' to support object auto inject!\")", AndroidLog, ClassName.get(parent));
       injectMethodBuilder.endControlFlow();
} else {
       njectMethodBuilder.addStatement(statement, StringUtils.isEmpty(fieldConfig.name()) ? fieldName : fieldConfig.name());
}

// 非空检查
if (fieldConfig.required() && !element.asType().getKind().isPrimitive()) {  // Primitive wont be check.
       injectMethodBuilder.beginControlFlow("if (null == substitute." + fieldName + ")");
       injectMethodBuilder.addStatement(
               "$T.e(\"" + Consts.TAG + "\", \"The field '" + fieldName + "' is null, in class '\" + $T.class.getName() + \"!\")", AndroidLog, ClassName.get(parent));
       injectMethodBuilder.endControlFlow();
}

上面的代码解析是IProvider以外的情况,通过分析我们又学到了新的知识:

@Autowired只能直接IProvider对象,或者是Activity和Fragment中使用!!!

并且知道Activity和Fragment的中的属性是通过intent和arguments来初始化的,我们已经完整的分析了整个@Autowired直接的解析过程,但是仍然有些懵逼,因为是源文件是通过代码写入的,所以最直观的方法就是看看源文件到底生成了什么。

首先看IProvider:

// 测试服务,其中HelloService实现了IProvider接口
public class TestService {

    @Autowired
    public HelloService helloService;

    public TestService(){
        ARouter.getInstance().inject(this);
    }
}

// 以下是生成的源文件
/**
 * DO NOT EDIT THIS FILE!!! IT WAS GENERATED BY AROUTER. */
public class TestService$$ARouter$$Autowired implements ISyringe {
  private SerializationService serializationService;

  @Override
  public void inject(Object target) {
    serializationService = ARouter.getInstance().navigation(SerializationService.class);
    TestService substitute = (TestService)target;
    substitute.helloService = ARouter.getInstance().navigation(HelloService.class);
  }
}

Activity:

@Route(path = "/main/TestActivity", extras = 1)
public class TestActivity extends AppCompatActivity {

    @Autowired
    String name;

    @Autowired
    int age;

    @Autowired
    TestBean testBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ARouter.getInstance().inject(this);
    }
}

// 生成文件代码
public class TestActivity$$ARouter$$Autowired implements ISyringe {
  private SerializationService serializationService;

  @Override
  public void inject(Object target) {
    serializationService = ARouter.getInstance().navigation(SerializationService.class);
    TestActivity substitute = (TestActivity)target;
    substitute.name = substitute.getIntent().getStringExtra("name");
    substitute.age = substitute.getIntent().getIntExtra("age", substitute.age);
    substitute.testBean = (com.lzp.arouter.activity.bean.TestBean) substitute.getIntent().getSerializableExtra("testBean");
  }
}

@Autowired注解已经分析完毕,通过同样的道理,我们还可以自行查看RouteProcessor,InterceptorProcessor的内容,最终都是生成了各种源文件保存注解中的信息,这里就不具体分析了。

arouter-api
这个就是ARouter的核心库了,通过刚才分析编译库的内容,我们可以很轻松的知道@Autowired的执行过程,例如:

@Route(path = "/main/TestActivity", extras = 1)
public class TestActivity extends AppCompatActivity {

    @Autowired
    String name;

    @Autowired
    int age;

    @Autowired
    TestBean testBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        // 必须调用该方法,才有有效
        ARouter.getInstance().inject(this);
    }
}

我们找到inject的最终调用层级在:

static void inject(Object thiz) {
        AutowiredService autowiredService = ((AutowiredService) ARouter.getInstance().build("/arouter/service/autowired").navigation());
        if (null != autowiredService) {
            autowiredService.autowire(thiz);
        }
    }

根据路由地址“/arouter/service/autowired”,我们在库中找到了AutowiredServiceImpl:

@Route(path = "/arouter/service/autowired")
public class AutowiredServiceImpl implements AutowiredService {
   ...
    @Override
    public void autowire(Object instance) {
        String className = instance.getClass().getName();
        try {
            if (!blackList.contains(className)) {
                ISyringe autowiredHelper = classCache.get(className);
                if (null == autowiredHelper) {  // No cache.
                	// 通过源文件名称的生成规则,找到生成的源文件
                    autowiredHelper = (ISyringe) Class.forName(instance.getClass().getName() + SUFFIX_AUTOWIRED).getConstructor().newInstance();
                }
                // 调用源文件的inject方法,完成自动装配的工作
                autowiredHelper.inject(instance);
                classCache.put(className, autowiredHelper);
            }
        } catch (Exception ex) {
            blackList.add(className);    // This instance need not autowired.
        }
    }
}

@Autowired的运行流程就是这样,其中最重要的就是

ARouter.getInstance().build("/arouter/service/autowired").navigation());

我们发现无论是Activity,Fragment还是Service,都是通过这个方法进行匹配的,搞定了他,ARouter的api库就算是大功告成了。

ARouter类的功能都是委托_ARouter类实现的,navigation方法被重载了很多种,但是核心的步骤只有三个:

1、找到路由地址,组装成Postcard对象
2、调用LogisticsCenter.completion(postcard),完善Postcard的信息
3、返回指定的对象或跳转。

1、找到路由地址,组装成Postcard对象

如果我们直接指定了路由地址,例如:

ARouter.getInstance().build("/arouter/service/autowired").navigation());

// 创建对应的Postcard
    protected Postcard build(String path, String group) {
        if (TextUtils.isEmpty(path) || TextUtils.isEmpty(group)) {
            throw new HandlerException(Consts.TAG + "Parameter is invalid!");
        } else {
            // 找到是否有自定义PathReplaceService
            PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
            if (null != pService) {
                path = pService.forString(path);
            }
            // 默认是没有的,所以会进入到这里
            // 如果没有指定group,默认的分组是第一个斜线的单词
            return new Postcard(path, group);
        }
    }

如果我们没有指定url地址,而是使用Class:

protected <T> T navigation(Class<? extends T> service) {
        try {
            // 通过编译生成的文件得到Postcard
            Postcard postcard = LogisticsCenter.buildProvider(service.getName());
            // 注释已经写了,是为了适配1.0.5的版本
            // Compatible 1.0.5 compiler sdk.
            // Earlier versions did not use the fully qualified name to get the service
            if (null == postcard) {
                // No service, or this service in old version.
                postcard = LogisticsCenter.buildProvider(service.getSimpleName());
            }

            if (null == postcard) {
                return null;
            }
            // 这是第二步了
            LogisticsCenter.completion(postcard);
            return (T) postcard.getProvider();
        } catch (NoRouteFoundException ex) {
            logger.warning(Consts.TAG, ex.getMessage());
            return null;
        }
    }

我们编译出的文件已经记录了Class和路由的对应信息,直接就可以使用。

调用LogisticsCenter.completion(postcard)

这个方法会通过Postcard中的路径信息,完成页面的查找操作:

public synchronized static void completion(Postcard postcard) {
        if (null == postcard) {
            throw new NoRouteFoundException(TAG + "No postcard!");
        }
		// 得到RouteMeta信息,这个在编译器中已经生成了
        RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath());
        // 一个空的检查,这里就先忽略了
        if (null == routeMeta) {    // Maybe its does't exist, or didn't load.
           ...
        } else {
        	// 通过RouteMeta,完善Postcard的信息
        	// 要找到的类名
            postcard.setDestination(routeMeta.getDestination());
            // 路由的类型
            postcard.setType(routeMeta.getType());
            // 优先级
            postcard.setPriority(routeMeta.getPriority());
            // extra,注解中的extra
            postcard.setExtra(routeMeta.getExtra());
			// 通过uri,解析路由的参数
            Uri rawUri = postcard.getUri();
            if (null != rawUri) {   // Try to set params into bundle.
                Map<String, String> resultMap = TextUtils.splitQueryParameters(rawUri);
                Map<String, Integer> paramsType = routeMeta.getParamsType();

                if (MapUtils.isNotEmpty(paramsType)) {
                    // 添加参数
                    for (Map.Entry<String, Integer> params : paramsType.entrySet()) {
                        setValue(postcard,
                                params.getValue(),
                                params.getKey(),
                                resultMap.get(params.getKey()));
                    }
                    postcard.getExtras().putStringArray(ARouter.AUTO_INJECT, paramsType.keySet().toArray(new String[]{}));
                }
                postcard.withString(ARouter.RAW_URI, rawUri.toString());
            }
			// 根据不同的类型,做一些补充操作
            switch (routeMeta.getType()) {
           		// 如果是IProvider,这里已经直接通过反射创建对象了
                case PROVIDER:  // if the route is provider, should find its instance
                    // Its provider, so it must implement IProvider
                    Class<? extends IProvider> providerMeta = (Class<? extends IProvider>) routeMeta.getDestination();
                    IProvider instance = Warehouse.providers.get(providerMeta);
                    if (null == instance) { // There's no instance of this provider
                        IProvider provider;
                        try {
                            provider = providerMeta.getConstructor().newInstance();
                            provider.init(mContext);
                            Warehouse.providers.put(providerMeta, provider);
                            instance = provider;
                        } catch (Exception e) {
                            throw new HandlerException("Init provider failed! " + e.getMessage());
                        }
                    }
                    // 设置属性,之后通过这个属性,得到创建的对象
                    postcard.setProvider(instance);
                    // greenChannel之前已经说过了,表示跳过interceptor
                    postcard.greenChannel();    // Provider should skip all of interceptors
                    break;
                case FRAGMENT:
                	// fragment也是跳过interceptor的
                    postcard.greenChannel();    // Fragment needn't interceptors
                default:
                    break;
            }
        }
    }

返回指定的对象或跳转

在第二步我们已经完善了Postcard的信息,接下来是第三步,这里有两种情况,如果我们直接使用的是:

protected <T> T navigation(Class<? extends T> service) {
        try {
            // 通过编译生成的文件得到Postcard
            Postcard postcard = LogisticsCenter.buildProvider(service.getName());
            // 注释已经写了,是为了适配1.0.5的版本
            // Compatible 1.0.5 compiler sdk.
            // Earlier versions did not use the fully qualified name to get the service
            if (null == postcard) {
                // No service, or this service in old version.
                postcard = LogisticsCenter.buildProvider(service.getSimpleName());
            }

            if (null == postcard) {
                return null;
            }
            // 完善Postcard信息
            LogisticsCenter.completion(postcard);
            // 返回指定对象
            return (T) postcard.getProvider();
        } catch (NoRouteFoundException ex) {
            logger.warning(Consts.TAG, ex.getMessage());
            return null;
        }
    }

通过刚才第二步的分析,我们可以推断出: navigation(Class<? extends T> service)仅仅适用于IProvider。

其他情况会调用:

protected Object navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
        try {
        	// 第二步,完善Postcard信息
            LogisticsCenter.completion(postcard);
        } catch (NoRouteFoundException ex) {
        	// 此处省略,会打印信息,回调callback的onlost方法
           ...
            return null;
        }
		// 回调onFound
        if (null != callback) {
            callback.onFound(postcard);
        }
		// 是否要被拦截
        if (!postcard.isGreenChannel()) {  
            interceptorService.doInterceptions(postcard, new InterceptorCallback() {
              
                @Override
                public void onContinue(Postcard postcard) {
                	// 继续
                    _navigation(context, postcard, requestCode, callback);
                }

                @Override
                public void onInterrupt(Throwable exception) {
                    if (null != callback) {
                        callback.onInterrupt(postcard);
                    }
                }
            });
        } else {
        	// 继续
            return _navigation(context, postcard, requestCode, callback);
        }
        return null;
    }

如果跳过或通过了拦截器就会调用_navigation(context, postcard, requestCode, callback)方法,这也是最后一个分析的方法了:

private Object _navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
        final Context currentContext = null == context ? mContext : context;
        switch (postcard.getType()) {
        	// 找到Activity,intent添加参数,startActivity
            case ACTIVITY:
                // Build intent
                final Intent intent = new Intent(currentContext, postcard.getDestination());
                intent.putExtras(postcard.getExtras());

                // Set flags.
                int flags = postcard.getFlags();
                if (-1 != flags) {
                    intent.setFlags(flags);
                } else if (!(currentContext instanceof Activity)) {    // Non activity, need less one flag.
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }

                // Set Actions
                String action = postcard.getAction();
                if (!TextUtils.isEmpty(action)) {
                    intent.setAction(action);
                }

                // 切换到主线程
                runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(requestCode, currentContext, intent, postcard, callback);
                    }
                });

                break;
                // 在第二步已经创建IProvider了,直接返回就可以了
            case PROVIDER:
                return postcard.getProvider();
                // 广播,Provider,Fragment
            case BOARDCAST:
            case CONTENT_PROVIDER:
            case FRAGMENT:
                Class fragmentMeta = postcard.getDestination();
                try {
                	// 通过返回创建指定对象,请注意这是无参构造方法
                    Object instance = fragmentMeta.getConstructor().newInstance();
                    // Fragment要添加参数
                    if (instance instanceof Fragment) {
                        ((Fragment) instance).setArguments(postcard.getExtras());
                    } else if (instance instanceof android.support.v4.app.Fragment) {
                        ((android.support.v4.app.Fragment) instance).setArguments(postcard.getExtras());
                    }

                    return instance;
                } catch (Exception ex) {
                    logger.error(Consts.TAG, "Fetch fragment instance error, " + TextUtils.formatStackTrace(ex.getStackTrace()));
                }
            case METHOD:
            case SERVICE:
            default:
                return null;
        }

        return null;
    }

到这里ARouter的源码分析就到此结束了。

总结

经过漫长的分析,我们了解了ARouter的实现原理,还学到了文档可能不会提及的用法,最后我们做一个总结:

  1. @Autowired只能注解非private属性
  2. @Autowired只能注解IProvider,或者在Activity和Fragment中使用。
  3. @Autowired注解IProvider会反射创建对象,Activity和Fragment中则通过intent和argument中的携带参数进行填充。
  4. navigation(Class clazz)方法仅适用于IProvider
  5. IProvider和Fragment默认跳过拦截器
  6. 如果interceptor要进行耗时操作,请开启异步线程,在主线程中可能会引起ANR


摘自:Android:从零开始打造自己的深度链接库(二):ARouter源码解析

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ARouter(二)源码解析 的相关文章

  • Databend 存储架构总览

    目的 通过本篇文章带大家理解一下 Databend 的存储结构 Databend 内置的 Table 引擎为 Fuse table engine 也是接下来要花重点篇幅要讲的 另外 Databend 还支持外置的 Hive table 及
  • win10病毒和威胁防护无法重新启动解决方法

    1 检查电脑中是否安装了任何的第三方反病毒软件 例如 360 腾讯电脑管家等 如果有的话 麻烦您将其卸载 卸载完毕后重启设备 再看一下病毒和威胁防护能否正常启动 2 按 Windows 徽标键 X 启动 Windows PowerShell
  • nofollow标签的作用 nofollow标签添加方法

    nofollow标签的作用 nofollow标签添加方法 nofollow标签是seo优化常用的一个标签 它的作用是告诉搜索引擎不要追踪这个链接 也就是阻止搜索引擎向这个网页或链接传递权重 nofollow有两种写法 1 将 nofollo
  • 第三章. Pandas入门—索引设置

    第三章 Pandas入门 3 8 索引设置 1 索引的作用 1 更方便的查询数据 2 使用索引可以提升查询性能 如果索引是唯一的 Pandas会使用哈希表优化 查找数据的时间复杂度为O 1 如果索引不是唯一的 但是有序 Pandas会使用二
  • 梯度下降函数理解

    r d 可以理解为有d的参数进行约束 或者 D 向量有d个维度 咱们将楼主的给的凸优化结构细化一点 别搞得那么抽象 不好解释 其中 咱们可以令 f ok 这个先介绍到这里 至于f x 为什么用多项式的方式去模拟 相信也是很多人的疑问 很简单
  • 组织关系图谱

    div style width 100 height 800px div
  • git强制提交本地分支覆盖掉远程分支

    语法比较简单 命令如下 git push origin 分支名 force 举个栗子 git push origin V2 2 3 force 运行结果 Total 0 delta 0 reused 0 delta 0 To http 19
  • golang-bufio 缓冲扫描

    前面两篇博客 介绍了 bufio 包中的缓冲读和写 bufio go 下面再来介绍一下缓冲扫描 scan go 这个扫描的是用来对缓存读的更高级封装 提供了一些更易用的方法 缓冲扫描 Scanner 提供了一个方便的接口来读取数据 例如使用
  • flask-会话机制

    使用flask bootstrap 步骤 1 pip install flask bootstrap 2 进行配置 from flask bootstrap import Bootstrap bootstrap Bootstrap 在 in
  • kafka 监控工具--CMAK

    CMAK previously known as Kafka Manager is a tool for managing Apache Kafka clusters See below for details about the name
  • 二分法总结(超级详细)附带图解

    文章目录 1 二分法 2 时间复杂度 3 二分法的套路 3 1 整数的二分 3 2 实数的划分 四 相关习题 4 1 数的范围 4 2 数的三次方根 1 二分法 二分查找是一个时间效率极高的算法 尤其是面对大量的数据时 其查找效率是极高 时
  • python读取npy文件时,太大不能完全显示,其解决方法

    python读取npy文件时 太大不能完全显示 其解决方法 当用python读取npy文件时 会遇到npy文件太大 用print函数打印时不能完全显示 如以下情况 其解决办法是 添加一行代码 np set printoptions thre
  • 2023汽车行业数字化转型报告

    目前 汽车行业正经历百年未有之大变局 在新一轮科技革命以及减碳 能源形势变革智能化变革推动下 汽车产业正由功能时代向智能时代演进 由 以车为中心 向 以用户为中心 转变 汽车的产品属性 产业价值链和生态结构都面临被颠覆 新的汽车市场格局正在
  • Python爬虫从入门到精通:(33)scrapy中间件_Python涛哥

    中间件 作用 批量拦截请求和响应 爬虫中间件 下载中间件 推荐 拦截请求 篡改请求url 伪装请求头信息 UA Cookie 设置代理 重点 拦截响应 篡改响应数据 详解 我们创建个工程middlePro 爬取百度和搜狗 import sc
  • goto编程练习

    for 的初始化要放到JUMP上边 不然i会一直为1 for 的i 也不能放到括号里边 不然i永远为0 1 include
  • 200smart第二课基本编程

    一 程序块 主程序main和子程序 主程序是执行程序的入口 没有主程序就不知道程序从哪里开始 子程序是一个大型程序中的某个代码 一般是完成某个算法 二 符号表 给变量定义 当我们在编程的时候 需要定义一些符号名称 如下图 程序运行 注释使程
  • MFC重载鼠标停留WM_MOUSEHOVER和离开WM_MOUSELEAVE消息

    1 重载OnMouseMove 消息 在消息的实现中添加代码 void CMainWindow OnMouseMove UINT nFlags CPoint point TRACKMOUSEEVENT tme tme cbSize size
  • 爬虫与反爬虫技术简介

    互联网的大数据时代的来临 网络爬虫也成了互联网中一个重要行业 它是一种自动获取网页数据信息的爬虫程序 是网站搜索引擎的重要组成部分 通过爬虫 可以获取自己想要的相关数据信息 让爬虫协助自己的工作 进而降低成本 提高业务成功率和提高业务效率
  • @JSONField 解决json字符串转对象,对象属性名与json中key不一致,如何接收数据问题

    背景 我有个对象 过来个json 想用这个对象接收json中的值 对象中属性名与json中key值不一致 实现 这个时候 JSONField注解就派上用场了 不能直接放在属性上 要放在set方法上 模拟 1 搞个对象 属性名分别为name

随机推荐

  • 【靶场】upload-labs Pass-02

    考纲 本pass在服务端对数据包的MIME进行检查 在右上角点击 查看提示 中看到 一 上一关 靶场 upload labs Pass 01 二 大马 介绍两款 php 大马 因为 一句话木马看不上 如果师傅有其他好用的 大马 还望师傅在评
  • QT添加qss文件和资源文件

    先右键项目 选择 Add New 选择一个模板 选择 Qt 模板 再选择 Qt Resource Files 点击 Choose 填上资源文件的名称 默认添加项目路径下 后面的步骤默认即可 点击完成 新建完成了资源文件后 默认会进入 res
  • 运放稳定性连载21:电容性负载的稳定性——具有双通道反馈的RISO(2)

    现在 我们必须测量如图10 6所示的Zo 小信号AC开环输出阻抗 该Tina SPICE测试电路将测试空载OPA177的Zo R2和R1以及LT为低通滤波器函数提供了一条AC通道 这样 使得我们能将DC短路和AC开路一起并入反馈电路 DC工
  • ssh报错no key alg(关于低版本连接高版本ssh)

    高版本 8 4 低版本 4 3 按照网上的方法试过 通过ssh keygen命令重新生成ssh主机秘钥 可以不用重启sshd服务 ssh keygen t rsa f etc ssh ssh host rsa key ssh keygen
  • NoReverseMatch: Reverse for ‘data‘ not found . ‘data‘ is not a valid view function or pattern

    Django gt python manage py runserver时报错 NoReverseMatch Reverse for data not found data is not a valid view func tion or
  • 制作一辆“自动驾驶”平衡自行车需要用到哪些知识

    目录 先看下小车效果 小车电路设计 相关软件工具 keil C语言设计编码调试工具 主要 mcuisp 代码烧录工具 一般使用一种烧录工具就可以 STM32 STlink stlink烧录工具 STM32 Cube pro 烧录工具 ope
  • C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现

    本文是转载的 正版是https blog twofei com 496 欢迎去看正版 C 中的虚函数 表 实现机制以及用C语言对其进行的模拟实现 前言 大家都应该知道C 的精髓是虚函数吧 虚函数带来的好处就是 可以定义一个基类的指针 其指向
  • OceanBase使用范例

    http www mysqlops com 2011 08 31 oceanbase use html OceanBase的使用类似于关系型数据库 需要预先创建schema 关于schema的格式 请参见schema说明 假如我们有以下sc
  • c#Socket 异步通讯(客户端与服务端)

    c Socket 异步通讯 多个客户端与服务端 最近公司有个项目 涉及到的通讯对象有点多 就拿其中一个库的通讯来说就用到了3个PLC 这里就涉及了一个服务器与多个客户端之间的通讯了 同时上位机既需要做客户端 也需要做服务端 因为跟PLC之间
  • HTTP响应详解, HTTP请求构造及HTTPS详解

    HTTP响应详解 认识 状态码 status code 状态码表示访问一个页面的结果 是访问成功 还是失败 还是其他的一些情况 以下为常见的状态码 200 OK 这 是一个最常见的状态码 表示访问成功 抓包抓到的大部分结果都是 200 例如
  • numpy load npz文件

    一 问题 numpy version 1 23 0 优化项目的是时候发现索引一个dict的时候很慢 因此进行分析 速度很慢的问题代码如下 arr dict np load test npz npz 100MB for i in range
  • 神州网信远程、关闭屏幕时间、关闭神州网信密码

    一 远程查看电脑 按 windows r 输入gpedit msc 运行组策略 gpedit msc 进行下面的操作 1 计算机配置 管理模板 Windows组件 远程桌面服务 远程桌面会话主机 连接 允许用户通过使用远程桌面服务进行远程连
  • Qt creator4.8.0 以上使用SqLite数据库进行数据操作

    文章目录 前言 一 在 pro工程文件中添加sql模块 二 使用步骤 1 添加头文件 2 链接并打开数据库 3 创建用户信息表management info 4 插入数据操作 5 修改数据库操作 6 查询数据库 总结 前言 Qt creat
  • 基于MATLAB的filter的使用,低通、带通和高通滤波器设计

    1 目的 学习MATLAB的filter函数的使用 通过设计低通 带通和高通滤波器对其进行仿真 2 用到的主要函数和工具 MATLAB FDATOOL filter fft 3 设计 信号的产生 Parameter Interface Fr
  • java高级开发面试题总结

    面试题总结 JAVA高级工程师 近期考虑换工作的问题 于是投简历面试 面试5家公司的高级Java工程师 有4家给了我offer 想着总结一下面试经验 方便最近正在寻求机会的你们 一 无笔试题 不知道是不是职位原因还是没遇到 面试时 都不需要
  • 阿里云轻量应用服务器使用指南适用于所有人

    最近一直在捣鼓阿里云服务器 想着把自己写好的一些项目部署到服务器上供其他人访问 一路上踩了不少坑 也查了不少资料 最后解决了 写个博客记录下来 也为其他想要建站的同学提供一个指引 购买轻量应用服务器 传送门 阿里云 如果是在校学生 可以直接
  • SpringCloud之Hystrix

    1 服务熔断与降级 在微服务架构中多层服务之间会相互调用 如果其中有一层服务故障了 可能会导致一层服务或者多层服务 故障 从而导致整个系统故障 这种现象被称为服务雪崩效应 SpringCloud 中的 Hystrix 组件就可以解决此类问题
  • 有时间再看decode详解

    Oracle 中 decode 函数用法 含义解释 decode 条件 值1 返回值1 值2 返回值2 值n 返回值n 缺省值 该函数的含义如下 IF 条件 值1 THEN RETURN 翻译值1 ELSIF 条件 值2 THEN RETU
  • 冲刺春招-精选笔面试 66 题大通关 day6

    day6题目 33 搜索旋转排序数组 54 螺旋矩阵 bytedance 006 夏季特惠 学习计划链接 冲刺春招 精选笔面试 66 题大通关 今日知识点 二分 模拟 01背包 难度为中等 中等 字节 简单 33 搜索旋转排序数组 整数数组
  • ARouter(二)源码解析

    前言 这一篇我们来具体看一下ARouter的实现原理 如果你之前没有接触过ARouter 可以先阅读上一篇 Android 从零开始打造自己的深度链接库 一 ARouter简介 废话不多 我们赶紧分析源码 正文 首先我们从github下载最