在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。为了达到这个目的,SpringBoot提供了ApplicationRunner接口和CommandLineRunner接口,这两个接口都有一个run()方法,在实现接口时需要覆盖该方法,不同之处在于ApplicationRunner的优先级大于CommandLineRunner,当然你可以给CommandLineRunner指定@Order(1)注解,使其优先级大于ApplicationRunner
    使ApplicationRunner接口与CommandLineRunner接口生效的方式有两种:

    • 自定义类实现接口
    • 主启动类实现接口 :::info 入参格式 ::: 启动参数的格式为--name=liuyanntes空格--email==liuyanntes@163.com ```java class SimpleCommandLineArgsParser {

      /**

      • Parse the given {@code String} array based on the rules described {@linkplain
      • SimpleCommandLineArgsParser above}, returning a fully-populated
      • {@link CommandLineArgs} object.
      • @param args command line arguments, typically from a {@code main()} method */ public CommandLineArgs parse(String… args) { CommandLineArgs commandLineArgs = new CommandLineArgs(); for (String arg : args) {
        1. if (arg.startsWith("--")) {
        2. String optionText = arg.substring(2);
        3. String optionName;
        4. String optionValue = null;
        5. int indexOfEqualsSign = optionText.indexOf('=');
        6. if (indexOfEqualsSign > -1) {
        7. optionName = optionText.substring(0, indexOfEqualsSign);
        8. optionValue = optionText.substring(indexOfEqualsSign + 1);
        9. }
        10. else {
        11. optionName = optionText;
        12. }
        13. if (optionName.isEmpty()) {
        14. throw new IllegalArgumentException("Invalid argument syntax: " + arg);
        15. }
        16. commandLineArgs.addOptionArg(optionName, optionValue);
        17. }
        18. else {
        19. commandLineArgs.addNonOptionArg(arg);
        20. }
        } return commandLineArgs; }

    }

    1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/750797/1650129058792-192f8ebb-4cc5-407c-aeb5-ac684e265b1e.png#averageHue=%233d4144&clientId=u22623086-4e38-4&from=paste&height=690&id=llheM&originHeight=690&originWidth=1092&originalType=binary&ratio=1&rotation=0&showTitle=false&size=70669&status=done&style=none&taskId=u4bf5ce73-256f-478f-821e-2ba1fa00470&title=&width=1092)
    2. :::info
    3. 自定义类实现接口
    4. :::
    5. 自定义`TestApplicationRunner`实现`ApplicationRunner`
    6. ```java
    7. package com.example.demo.runner;
    8. import org.springframework.boot.ApplicationArguments;
    9. import org.springframework.boot.ApplicationRunner;
    10. import org.springframework.stereotype.Component;
    11. import java.util.Arrays;
    12. @Component
    13. public class TestApplicationRunner implements ApplicationRunner {
    14. @Override
    15. public void run(ApplicationArguments args) {
    16. System.out.println("===MyApplicationRunner===" + Arrays.asList(args.getSourceArgs()));
    17. System.out.println("===getOptionNames========" + args.getOptionNames());
    18. System.out.println("===name=======" + args.getOptionValues("name"));
    19. System.out.println("==email========" + args.getOptionValues("email"));
    20. }
    21. }

    再创建一个TestCommandLineRunner实现CommandLineRunner

    1. package com.example.demo.runner;
    2. import org.springframework.boot.CommandLineRunner;
    3. import org.springframework.stereotype.Component;
    4. import java.util.Arrays;
    5. @Component
    6. public class TestCommandLineRunner implements CommandLineRunner {
    7. @Override
    8. public void run(String... args) {
    9. System.out.println("===CommandLineRunner===" + Arrays.asList(args));
    10. }
    11. }

    主启动类DemoApplication

    1. package com.example.demo;
    2. import org.springframework.boot.CommandLineRunner;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. import java.util.Arrays;
    6. @SpringBootApplication
    7. public class DemoApplication{
    8. public static void main(String[] args) {
    9. SpringApplication.run(DemoApplication.class, args);
    10. System.out.println("主启动类结束");
    11. }
    12. }

    启动日志

    1. D:\App\java1.8\jdk\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always "-javaagent:D:\App\IntelliJ IDEA 2020.3.1\lib\idea_rt.jar=58988:D:\App\IntelliJ IDEA 2020.3.1\bin" -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Dfile.encoding=UTF-8 -classpath D:\App\java1.8\jdk\jre\lib\charsets.jar;D:\App\java1.8\jdk\jre\lib\deploy.jar;D:\App\java1.8\jdk\jre\lib\ext\access-bridge-64.jar;D:\App\java1.8\jdk\jre\lib\ext\cldrdata.jar;D:\App\java1.8\jdk\jre\lib\ext\dnsns.jar;D:\App\java1.8\jdk\jre\lib\ext\jaccess.jar;D:\App\java1.8\jdk\jre\lib\ext\jfxrt.jar;D:\App\java1.8\jdk\jre\lib\ext\localedata.jar;D:\App\java1.8\jdk\jre\lib\ext\nashorn.jar;D:\App\java1.8\jdk\jre\lib\ext\sunec.jar;D:\App\java1.8\jdk\jre\lib\ext\sunjce_provider.jar;D:\App\java1.8\jdk\jre\lib\ext\sunmscapi.jar;D:\App\java1.8\jdk\jre\lib\ext\sunpkcs11.jar;D:\App\java1.8\jdk\jre\lib\ext\zipfs.jar;D:\App\java1.8\jdk\jre\lib\javaws.jar;D:\App\java1.8\jdk\jre\lib\jce.jar;D:\App\java1.8\jdk\jre\lib\jfr.jar;D:\App\java1.8\jdk\jre\lib\jfxswt.jar;D:\App\java1.8\jdk\jre\lib\jsse.jar;D:\App\java1.8\jdk\jre\lib\management-agent.jar;D:\App\java1.8\jdk\jre\lib\plugin.jar;D:\App\java1.8\jdk\jre\lib\resources.jar;D:\App\java1.8\jdk\jre\lib\rt.jar;D:\devspace\test\target\classes;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-web\2.6.5\spring-boot-starter-web-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter\2.6.5\spring-boot-starter-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot\2.6.5\spring-boot-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-autoconfigure\2.6.5\spring-boot-autoconfigure-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-logging\2.6.5\spring-boot-starter-logging-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\devspace\apache-maven-3.3.9\myRepository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\devspace\apache-maven-3.3.9\myRepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-json\2.6.5\spring-boot-starter-json-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\core\jackson-databind\2.13.2\jackson-databind-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\core\jackson-annotations\2.13.2\jackson-annotations-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\core\jackson-core\2.13.2\jackson-core-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.2\jackson-datatype-jdk8-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.2\jackson-datatype-jsr310-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.2\jackson-module-parameter-names-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-tomcat\2.6.5\spring-boot-starter-tomcat-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.60\tomcat-embed-core-9.0.60.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\tomcat\embed\tomcat-embed-el\9.0.60\tomcat-embed-el-9.0.60.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.60\tomcat-embed-websocket-9.0.60.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-web\5.3.17\spring-web-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-beans\5.3.17\spring-beans-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-webmvc\5.3.17\spring-webmvc-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-aop\5.3.17\spring-aop-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-context\5.3.17\spring-context-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-expression\5.3.17\spring-expression-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-core\5.3.17\spring-core-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-jcl\5.3.17\spring-jcl-5.3.17.jar com.example.demo.DemoApplication --name=liuyanntes --email==liuyanntes@163.com
    2. . ____ _ __ _ _
    3. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
    4. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    5. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
    6. ' |____| .__|_| |_|_| |_\__, | / / / /
    7. =========|_|==============|___/=/_/_/_/
    8. :: Spring Boot :: (v2.6.5)
    9. 2022-04-17 00:59:49.608 INFO 5576 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_202 on DESKTOP-13536KB with PID 5576 (D:\devspace\test\target\classes started by dell in D:\devspace\test)
    10. 2022-04-17 00:59:49.608 INFO 5576 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
    11. 2022-04-17 00:59:50.813 INFO 5576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
    12. 2022-04-17 00:59:50.828 INFO 5576 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
    13. 2022-04-17 00:59:50.828 INFO 5576 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60]
    14. 2022-04-17 00:59:50.969 INFO 5576 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
    15. 2022-04-17 00:59:50.969 INFO 5576 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1267 ms
    16. 2022-04-17 00:59:51.344 INFO 5576 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
    17. 2022-04-17 00:59:51.344 INFO 5576 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.272 seconds (JVM running for 3.351)
    18. ===MyApplicationRunner===[--name=liuyanntes, --email==liuyanntes@163.com]
    19. ===getOptionNames========[name, email]
    20. ===name=======[liuyanntes]
    21. ==email========[=liuyanntes@163.com]
    22. ===CommandLineRunner===[--name=liuyanntes, --email==liuyanntes@163.com]
    23. 主启动类结束

    执行顺序为:

    1. 主程序启动
    2. 执行TestApplicationRunner.run,
    3. 执行TestCommandLineRunner.run,
    4. 主启动类结束 :::info 主启动类实现接口 ::: 令主启动类DemoApplication去实现TestApplicationRunner或者TestCommandLineRunner
      主启动类实现ApplicationRunner: ```java package com.example.demo;

    import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;

    import java.util.Arrays;

    @SpringBootApplication public class DemoApplication implements ApplicationRunner { //主类注释 //分支合并分支合并分支合并 public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); System.out.println(“主启动类结束”); }

    1. @Override
    2. public void run(ApplicationArguments args) throws Exception {
    3. System.out.println("这是重写的启动类run方法"+ Arrays.asList(args));
    4. System.out.println("===MyApplicationRunner===" + Arrays.asList(args.getSourceArgs()));
    5. System.out.println("===getOptionNames========" + args.getOptionNames());
    6. System.out.println("===name=======" + args.getOptionValues("name"));
    7. System.out.println("==email========" + args.getOptionValues("email"));
    8. }

    }

    启动日志: D:\App\java1.8\jdk\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always “-javaagent:D:\App\IntelliJ IDEA 2020.3.1\lib\idea_rt.jar=59676:D:\App\IntelliJ IDEA 2020.3.1\bin” -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Dfile.encoding=UTF-8 -classpath D:\App\java1.8\jdk\jre\lib\charsets.jar;D:\App\java1.8\jdk\jre\lib\deploy.jar;D:\App\java1.8\jdk\jre\lib\ext\access-bridge-64.jar;D:\App\java1.8\jdk\jre\lib\ext\cldrdata.jar;D:\App\java1.8\jdk\jre\lib\ext\dnsns.jar;D:\App\java1.8\jdk\jre\lib\ext\jaccess.jar;D:\App\java1.8\jdk\jre\lib\ext\jfxrt.jar;D:\App\java1.8\jdk\jre\lib\ext\localedata.jar;D:\App\java1.8\jdk\jre\lib\ext\nashorn.jar;D:\App\java1.8\jdk\jre\lib\ext\sunec.jar;D:\App\java1.8\jdk\jre\lib\ext\sunjce_provider.jar;D:\App\java1.8\jdk\jre\lib\ext\sunmscapi.jar;D:\App\java1.8\jdk\jre\lib\ext\sunpkcs11.jar;D:\App\java1.8\jdk\jre\lib\ext\zipfs.jar;D:\App\java1.8\jdk\jre\lib\javaws.jar;D:\App\java1.8\jdk\jre\lib\jce.jar;D:\App\java1.8\jdk\jre\lib\jfr.jar;D:\App\java1.8\jdk\jre\lib\jfxswt.jar;D:\App\java1.8\jdk\jre\lib\jsse.jar;D:\App\java1.8\jdk\jre\lib\management-agent.jar;D:\App\java1.8\jdk\jre\lib\plugin.jar;D:\App\java1.8\jdk\jre\lib\resources.jar;D:\App\java1.8\jdk\jre\lib\rt.jar;D:\devspace\test\target\classes;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-web\2.6.5\spring-boot-starter-web-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter\2.6.5\spring-boot-starter-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot\2.6.5\spring-boot-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-autoconfigure\2.6.5\spring-boot-autoconfigure-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-logging\2.6.5\spring-boot-starter-logging-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\devspace\apache-maven-3.3.9\myRepository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\devspace\apache-maven-3.3.9\myRepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-json\2.6.5\spring-boot-starter-json-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\core\jackson-databind\2.13.2\jackson-databind-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\core\jackson-annotations\2.13.2\jackson-annotations-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\core\jackson-core\2.13.2\jackson-core-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.2\jackson-datatype-jdk8-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.2\jackson-datatype-jsr310-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.2\jackson-module-parameter-names-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-tomcat\2.6.5\spring-boot-starter-tomcat-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.60\tomcat-embed-core-9.0.60.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\tomcat\embed\tomcat-embed-el\9.0.60\tomcat-embed-el-9.0.60.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.60\tomcat-embed-websocket-9.0.60.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-web\5.3.17\spring-web-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-beans\5.3.17\spring-beans-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-webmvc\5.3.17\spring-webmvc-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-aop\5.3.17\spring-aop-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-context\5.3.17\spring-context-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-expression\5.3.17\spring-expression-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-core\5.3.17\spring-core-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-jcl\5.3.17\spring-jcl-5.3.17.jar com.example.demo.DemoApplication —name=liuyanntes —email==liuyanntes@163.com

    . _ /\ / () \ \ \ \ ( ( )__ | ‘ | ‘| | ‘ \/ ` | \ \ \ \ \/ )| |)| | | | | || (| | ) ) ) ) ‘ |_| .|| ||| |\, | / / / / =========||==============|__/=///_/ :: Spring Boot :: (v2.6.5)

    2022-04-17 01:17:53.939 INFO 6812 —- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_202 on DESKTOP-13536KB with PID 6812 (D:\devspace\test\target\classes started by dell in D:\devspace\test) 2022-04-17 01:17:53.943 INFO 6812 —- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: “default” 2022-04-17 01:17:55.260 INFO 6812 —- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-04-17 01:17:55.273 INFO 6812 —- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-04-17 01:17:55.274 INFO 6812 —- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60] 2022-04-17 01:17:55.425 INFO 6812 —- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-04-17 01:17:55.425 INFO 6812 —- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1414 ms 2022-04-17 01:17:55.924 INFO 6812 —- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘’ 2022-04-17 01:17:55.936 INFO 6812 —- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.604 seconds (JVM running for 4.239) 这是重写的启动类run方法[org.springframework.boot.DefaultApplicationArguments@6dc1484] ===MyApplicationRunner===[—name=liuyanntes, —email==liuyanntes@163.com] ===getOptionNames========[name, email] ===name=======[liuyanntes] ==email========[=liuyanntes@163.com] 主启动类结束

    1. 主启动类实现`CommandLineRunner`
    2. ```java
    3. package com.example.demo;
    4. import org.springframework.boot.CommandLineRunner;
    5. import org.springframework.boot.SpringApplication;
    6. import org.springframework.boot.autoconfigure.SpringBootApplication;
    7. import java.util.Arrays;
    8. @SpringBootApplication
    9. public class DemoApplication implements CommandLineRunner{
    10. //主类注释
    11. //分支合并分支合并分支合并
    12. public static void main(String[] args) {
    13. SpringApplication.run(DemoApplication.class, args);
    14. System.out.println("主启动类结束");
    15. }
    16. @Override
    17. public void run(String... args) throws Exception {
    18. System.out.println("这是重写的启动类run方法"+ Arrays.asList(args));
    19. }
    20. }
    21. 日志打印
    22. D:\App\java1.8\jdk\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always "-javaagent:D:\App\IntelliJ IDEA 2020.3.1\lib\idea_rt.jar=59541:D:\App\IntelliJ IDEA 2020.3.1\bin" -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -Dfile.encoding=UTF-8 -classpath D:\App\java1.8\jdk\jre\lib\charsets.jar;D:\App\java1.8\jdk\jre\lib\deploy.jar;D:\App\java1.8\jdk\jre\lib\ext\access-bridge-64.jar;D:\App\java1.8\jdk\jre\lib\ext\cldrdata.jar;D:\App\java1.8\jdk\jre\lib\ext\dnsns.jar;D:\App\java1.8\jdk\jre\lib\ext\jaccess.jar;D:\App\java1.8\jdk\jre\lib\ext\jfxrt.jar;D:\App\java1.8\jdk\jre\lib\ext\localedata.jar;D:\App\java1.8\jdk\jre\lib\ext\nashorn.jar;D:\App\java1.8\jdk\jre\lib\ext\sunec.jar;D:\App\java1.8\jdk\jre\lib\ext\sunjce_provider.jar;D:\App\java1.8\jdk\jre\lib\ext\sunmscapi.jar;D:\App\java1.8\jdk\jre\lib\ext\sunpkcs11.jar;D:\App\java1.8\jdk\jre\lib\ext\zipfs.jar;D:\App\java1.8\jdk\jre\lib\javaws.jar;D:\App\java1.8\jdk\jre\lib\jce.jar;D:\App\java1.8\jdk\jre\lib\jfr.jar;D:\App\java1.8\jdk\jre\lib\jfxswt.jar;D:\App\java1.8\jdk\jre\lib\jsse.jar;D:\App\java1.8\jdk\jre\lib\management-agent.jar;D:\App\java1.8\jdk\jre\lib\plugin.jar;D:\App\java1.8\jdk\jre\lib\resources.jar;D:\App\java1.8\jdk\jre\lib\rt.jar;D:\devspace\test\target\classes;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-web\2.6.5\spring-boot-starter-web-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter\2.6.5\spring-boot-starter-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot\2.6.5\spring-boot-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-autoconfigure\2.6.5\spring-boot-autoconfigure-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-logging\2.6.5\spring-boot-starter-logging-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\devspace\apache-maven-3.3.9\myRepository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\devspace\apache-maven-3.3.9\myRepository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-json\2.6.5\spring-boot-starter-json-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\core\jackson-databind\2.13.2\jackson-databind-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\core\jackson-annotations\2.13.2\jackson-annotations-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\core\jackson-core\2.13.2\jackson-core-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.2\jackson-datatype-jdk8-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.2\jackson-datatype-jsr310-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.2\jackson-module-parameter-names-2.13.2.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\boot\spring-boot-starter-tomcat\2.6.5\spring-boot-starter-tomcat-2.6.5.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\tomcat\embed\tomcat-embed-core\9.0.60\tomcat-embed-core-9.0.60.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\tomcat\embed\tomcat-embed-el\9.0.60\tomcat-embed-el-9.0.60.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.60\tomcat-embed-websocket-9.0.60.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-web\5.3.17\spring-web-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-beans\5.3.17\spring-beans-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-webmvc\5.3.17\spring-webmvc-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-aop\5.3.17\spring-aop-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-context\5.3.17\spring-context-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-expression\5.3.17\spring-expression-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-core\5.3.17\spring-core-5.3.17.jar;D:\devspace\apache-maven-3.3.9\myRepository\org\springframework\spring-jcl\5.3.17\spring-jcl-5.3.17.jar com.example.demo.DemoApplication --name=liuyanntes --email==liuyanntes@163.com
    23. . ____ _ __ _ _
    24. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
    25. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    26. \\/ ___)| |_)| | | | | || (_| | ) ) ) )
    27. ' |____| .__|_| |_|_| |_\__, | / / / /
    28. =========|_|==============|___/=/_/_/_/
    29. :: Spring Boot :: (v2.6.5)
    30. 2022-04-17 01:14:19.875 INFO 492 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication using Java 1.8.0_202 on DESKTOP-13536KB with PID 492 (D:\devspace\test\target\classes started by dell in D:\devspace\test)
    31. 2022-04-17 01:14:19.875 INFO 492 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to 1 default profile: "default"
    32. 2022-04-17 01:14:21.526 INFO 492 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
    33. 2022-04-17 01:14:21.542 INFO 492 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
    34. 2022-04-17 01:14:21.542 INFO 492 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60]
    35. 2022-04-17 01:14:21.697 INFO 492 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
    36. 2022-04-17 01:14:21.712 INFO 492 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1689 ms
    37. 2022-04-17 01:14:22.103 INFO 492 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
    38. 2022-04-17 01:14:22.103 INFO 492 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 2.886 seconds (JVM running for 3.95)
    39. 这是重写的启动类run方法[--name=liuyanntes, --email==liuyanntes@163.com]
    40. 主启动类结束