spring boot 命令行启动的方式

 更新时间:2019年03月17日 10:02:11   作者:posuoren  
这篇文章主要介绍了spring boot 命令行启动的方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

在使用spring boot 构建应用启动时,我们在工作中都是通过命令行来启动应用,有时候会需要一些特定的参数以在应用启动时,做一些初始化的操作。

spring boot 提供了 CommandLineRunner 和 ApplicationRunner 这两个接口供用户使用。

1. CommandLineRunner

1.1 声明:

@FunctionalInterface
public interface CommandLineRunner {

  /**
   * Callback used to run the bean.
   * @param args incoming main method arguments
   * @throws Exception on error
   */
  void run(String... args) throws Exception;

}

1.2 使用:

package com.example.consoleapplication;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class TestRunner implements CommandLineRunner {

  @Override
  public void run(String... args) {
    // Do something...
    for(String arg: args){
      System.out.println(arg);
    }
    System.out.print("test command runner");
  }
}

1.3 运行结果

运行: java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -sdfsaf sdfas,

结果如下:

2019-03-16 17:31:56.544  INFO 18679 --- [           main] c.e.consoleapplication.DemoApplication   : No active profile set, falling back to default profiles: default
2019-03-16 17:31:57.195  INFO 18679 --- [           main] c.e.consoleapplication.DemoApplication   : Started DemoApplication in 16.172 seconds (JVM running for 16.65)
-sdfsaf
sdfas
test command runner%

2. ApplicationRunner

2.1 声明

/**
 * Interface used to indicate that a bean should <em>run</em> when it is contained within
 * a {@link SpringApplication}. Multiple {@link ApplicationRunner} beans can be defined
 * within the same application context and can be ordered using the {@link Ordered}
 * interface or {@link Order @Order} annotation.
 *
 * @author Phillip Webb
 * @since 1.3.0
 * @see CommandLineRunner
 */
@FunctionalInterface
public interface ApplicationRunner {

  /**
   * Callback used to run the bean.
   * @param args incoming application arguments
   * @throws Exception on error
   */
  void run(ApplicationArguments args) throws Exception;

}

2.2 使用

ApplicationRunner 和 CommandLineRunner 的使用是有差别的:

  • CommandLineRunner 的使用,只是把参数根据空格分割。
  • ApplicationRunner 会根据 是否匹配 --key=value 来解析参数,
    • 能匹配,则为 optional 参数, 可用getOptionValues获取参数值。
    • 不匹配则是 non optional 参数。
package com.example.consoleapplication;

import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.boot.ApplicationArguments;

@Component
public class TestApplicationRunner implements ApplicationRunner {

  @Override
  public void run(ApplicationArguments args) throws Exception {
    // Do something...
    System.out.println("option arg names" + args.getOptionNames());
    System.out.println("non option+" + args.getNonOptionArgs());
  }
}

2.3 运行结果

运行命令 java -jar build/libs/consoleapplication-0.0.1-SNAPSHOT.jar -non1 non2 --option=1, 结果为:

2019-03-16 18:08:08.528  INFO 19778 --- [           main] c.e.consoleapplication.DemoApplication   : No active profile set, falling back to default profiles: default
2019-03-16 18:08:09.166  INFO 19778 --- [           main] c.e.consoleapplication.DemoApplication   : Started DemoApplication in 16.059 seconds (JVM running for 16.56)
test
option arg names[option]
non option+[-non1, non2]-non1
non2
--option=1
test%

可以看到, optional 参数名有 option, non optional 参数有 -non1 和 non2

3. 小结

CommandLineRunner 和 ApplicationRunner 都能实现命令行应用启动时根据参数获取我们需要的值,做特殊的逻辑。但两者有所不同,推荐使用 ApplicationRunner 的 optional 参数, 方便扩展。

4. 参考文档

https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-web-environment

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java 通过AQS实现数据组织

    Java 通过AQS实现数据组织

    这篇文章主要介绍了通过AQS实现数据组织,想了解AQS的同学可以参考下
    2021-04-04
  • dubbo服务注册到nacos的过程剖析

    dubbo服务注册到nacos的过程剖析

    这篇文章主要为大家介绍了dubbo服务注册到nacos的过程剖析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职极限
    2022-02-02
  • Java中为什么start方法不能重复调用而run方法可以?

    Java中为什么start方法不能重复调用而run方法可以?

    这篇文章主要介绍了Java中为什么start方法不能重复调用而run方法可以?带着疑问一起学习下面文章的详细内容吧
    2022-05-05
  • Spring 中 PageHelper 不生效问题及解决方法

    Spring 中 PageHelper 不生效问题及解决方法

    这篇文章主要介绍了Spring 中 PageHelper 不生效问题,使用这个插件时要注意版本的问题,不同的版本可能 PageHelper 不会生效,本文结合示例代码给大家介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • Jackson自定义序列化与反序列化注解详解

    Jackson自定义序列化与反序列化注解详解

    这篇文章主要介绍了Jackson自定义序列化与反序列化注解详解,某些场景下,我们使用Jackson对数据进行序列化或反序列化的时候,需要对某些数据进行特殊处理,需要的朋友可以参考下
    2023-11-11
  • Guava轻松创建和管理不可变集合方法技巧

    Guava轻松创建和管理不可变集合方法技巧

    这篇文章主要为大家介绍了Guava轻松创建和管理不可变集合方法技巧示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • java中throws与try...catch的区别点

    java中throws与try...catch的区别点

    在本篇文章里小编给大家整理了一篇关于java中throws与try...catch的区别点的内容,需要的朋友们跟着学习下。
    2020-02-02
  • spring定义和装配bean详解

    spring定义和装配bean详解

    这篇文章主要介绍了spring定义和装配bean详解,具有一定参考价值,需要的朋友可以了解下。
    2017-12-12
  • Java利用读写的方式实现音频播放代码实例

    Java利用读写的方式实现音频播放代码实例

    这篇文章主要介绍了Java利用读写的方式实现音频播放代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • 基于SpringBoot实现定时发送邮件过程解析

    基于SpringBoot实现定时发送邮件过程解析

    这篇文章主要介绍了基于SpringBoot实现定时发送邮件过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06

最新评论