Spring Boot 启动器
本文最后更新于 933 天前,其中的信息可能已经有所发展或是发生改变。

Spring Boot 启动后执行,其实CommandLineRunnerApplicationRunner并无太大区别,前者通过数组接收启动参数,后者通过ApplicationArguments对象封装了启动参数。

CommandLineRunner

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class CustomRunner implements CommandLineRunner {
    @Override
    public void run(String... args) {
        System.out.println(args);
    }
}

ApplicationRunner

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class CustomRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) {
        args.getOptionNames().forEach(e -> System.out.printf("key: %s, value: %s%n", e, args.getOptionValues(e).toString()));
    }
}
如果觉得本文对您有帮助,记得收藏哦~
上一篇