首页 > 编程语言 > 详细

SpringBoot之启动加载器

时间:2020-03-17 15:25:44      阅读:73      评论:0      收藏:0      [点我收藏+]

一、什么是启动加载器?

在项目启动的时候做一些初始化工作。

二、启动类加载器实践

2.1 实现 CommandLineRunner 接口

@Component
public class FirstCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("run FirstCommandLineRunner");
    }
}

2.2 实现 ApplicationRunner 接口

@Component
public class FirstApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("run FirstApplicationRunner");
    }
}

启动项目,观察控制台输出:

run FirstApplicationRunner
run FirstCommandLineRunner

可以看到默认实现 ApplicationRunner 接口比 CommandLineRunner 优先级高。

2.3 使用 @Order 注解

我们试试看增加 @Order 注解之后会怎么样

@Component
@Order(1)
public class FirstCommandLineRunner implements CommandLineRunner {
    ......
}    
@Component
@Order(2)
public class FirstApplicationRunner implements ApplicationRunner {
    ......
}    

启动项目,观察控制台输出:

run FirstCommandLineRunner
run FirstApplicationRunner

发现 @Order 注解可以改变执行顺序。

三、启动类加载器原理

进入 SpringApplication.run 方法中,callRunners 方法就是入口:

public ConfigurableApplicationContext run(String... args) {
    ......
    callRunners(context, applicationArguments);
    ......
}

查看 callRunners 方法实现:

private void callRunners(ApplicationContext context, ApplicationArguments args) {
    List<Object> runners = new ArrayList<>();
    // 添加接口实现类
    runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
    runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
    // 进行排序
    AnnotationAwareOrderComparator.sort(runners);
    // 循环调用 callRunner 方法
    for (Object runner : new LinkedHashSet<>(runners)) {
        if (runner instanceof ApplicationRunner) {
            callRunner((ApplicationRunner) runner, args);
        }
        if (runner instanceof CommandLineRunner) {
            callRunner((CommandLineRunner) runner, args);
        }
    }
}

可以看到先添加的 ApplicationRunner 接口实现,然后添加 CommandLineRunner 接口实现,所以默认优先级以 ApplicationRunner 为先,后面有个排序实现,以 @Order 进行优先级排序

我们再来分别看下两个接口各自的 callRunner 的实现:

private void callRunner(ApplicationRunner runner, ApplicationArguments args) {
    try {
        (runner).run(args);
    }
    catch (Exception ex) {
        throw new IllegalStateException("Failed to execute ApplicationRunner", ex);
    }
}

private void callRunner(CommandLineRunner runner, ApplicationArguments args) {
    try {
        (runner).run(args.getSourceArgs());
    }
    catch (Exception ex) {
        throw new IllegalStateException("Failed to execute CommandLineRunner", ex);
    }
}   

SpringBoot之启动加载器

原文:https://www.cnblogs.com/markLogZhu/p/12510829.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!