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方法签名为何不包含返回值类型

    Java方法签名为何不包含返回值类型

    这篇文章主要介绍了Java方法签名为何不包含返回值类型,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Spring Boot使用线程池创建多线程的完整示例

    Spring Boot使用线程池创建多线程的完整示例

    在 Spring Boot 2 中,可以使用 @Autowired 注入 线程池(ThreadPoolTaskExecutor 或 ExecutorService),从而管理线程的创建和执行,以下是使用 @Autowired 方式注入线程池的完整示例,感兴趣的朋友一起看看吧
    2025-03-03
  • java中InputStream转为MultipartFile的解决方案

    java中InputStream转为MultipartFile的解决方案

    这篇文章主要介绍了java中InputStream转为MultipartFile的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-03-03
  • SpringMVC解析JSON请求数据问题解析

    SpringMVC解析JSON请求数据问题解析

    这篇文章主要介绍了SpringMVC解析JSON请求数据问题解析,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • SpringBoot调用service层的三种方法

    SpringBoot调用service层的三种方法

    在Spring Boot中,我们可以通过注入Service层对象来调用Service层的方法,Service层是业务逻辑的处理层,它通常包含了对数据的增删改查操作,本文给大家介绍了SpringBoot调用service层的三种方法,需要的朋友可以参考下
    2024-05-05
  • 将本地JAR文件手动添加到Maven本地仓库的实现过程

    将本地JAR文件手动添加到Maven本地仓库的实现过程

    在Java开发中,使用Maven作为项目管理工具已经成为了主流的选择,Maven提供了强大的依赖管理功能,可以轻松地下载和管理项目所需的库和工具,在某些情况下,你可能会需要将本地下载的JAR文件手动添加到Maven的本地仓库中,这篇博客将详细介绍如何实现这一过程
    2024-10-10
  • SpringBoot 整合 RabbitMQ 的使用方式(代码示例)

    SpringBoot 整合 RabbitMQ 的使用方式(代码示例)

    本文详细介绍了使用RabbitTemplate进行消息传递的几种模式,包括点对点通信、发布/订阅模式、工作队列模式、路由模式和主题模式,每种模式都通过代码示例展示了生产者和消费者的实现,帮助开发者理解和运用RabbitMQ进行高效的消息处理
    2024-10-10
  • springboot @ConditionalOnMissingBean注解的作用详解

    springboot @ConditionalOnMissingBean注解的作用详解

    这篇文章主要介绍了springboot @ConditionalOnMissingBean注解的作用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Java多线程之异步Future机制的原理和实现

    Java多线程之异步Future机制的原理和实现

    这篇文章主要为大家详细介绍了Java多线程之异步Future机制的原理和实现,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • Java中的static和final关键字的使用详解

    Java中的static和final关键字的使用详解

    这篇文章主要介绍了Java中的static和final关键字的使用详解,  当方法名前有static,即为static方法,可以方便我们无需创建对象也可以调用此方法,静态方法比较拉,只可以访问 静态的 属性/变量/方法,无法访问非静态的这些属性/变量/方法,需要的朋友可以参考下
    2024-01-01

最新评论