spring boot afterRefresh流程

spring boot使用的是1.5.9

 private void callRunners(ApplicationContext context, ApplicationArguments args) {
        List runners = new ArrayList();
        runners.addAll(context.getBeansOfType(ApplicationRunner.class).values());
        runners.addAll(context.getBeansOfType(CommandLineRunner.class).values());
        AnnotationAwareOrderComparator.sort(runners);
        Iterator var4 = (new LinkedHashSet(runners)).iterator();

        while(var4.hasNext()) {
            Object runner = var4.next();
            if (runner instanceof ApplicationRunner) {
                this.callRunner((ApplicationRunner)runner, args);
            }

            if (runner instanceof CommandLineRunner) {
                this.callRunner((CommandLineRunner)runner, args);
            }
        }

    }
 
  

可以看出来这里会找ApplicationRunner和CommandLineRunner的实现类,去运行他们在这个过程中可以进行一些操作

先看看CommandLineRunner类

@Component
public class OtherOperation implements CommandLineRunner {

    @Override
    public void run(String... strings) throws Exception {

        System.out.println("qwe");

        for (String s : strings){
            System.out.println(s);
        }

    }

}

大致是这样的,他可以获取你传进来的参数然后做一些自定义的操作。

如果我启动命令java -jar xxx.jar username=xxx,那么传进来的参数就是username=xxx,你可以对这个参数做一些操作等等。

当然用命令行传参数的优先级高于.yml和.properties(切忌有坑)

 

你可能感兴趣的:(spring,boot)