资讯专栏INFORMATION COLUMN

spring boot学习(4): 命令行启动

Binguner / 1100人阅读

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

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

spring boot 提供了 CommandLineRunnerApplicationRunner 这两个接口供用户使用。

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 run 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 使用

ApplicationRunnerCommandLineRunner 的使用是有差别的:

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 参数有 -non1non2

3. 小结

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

4. 参考文档
https://docs.spring.io/spring...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73738.html

相关文章

  • Spring Boot 应用 Docker 化 《Spring Boot 2.0极简教程》(陈光剑)

    摘要:应用化极简教程陈光剑基于的企业级应用开发最佳实践前面的章节中,我们都是在环境中开发运行测试应用程序。关键字是分布式应用微服务容器虚拟化。通常,在企业项目实践中,会实现一套应用部署发布的自动化运维平台工具。 Spring Boot 应用 Docker 化 《Spring Boot 2.0极简教程》(陈光剑)—— 基于 Gradle + Kotlin的企业级应用开发最佳实践 前面的章节中,...

    Donne 评论0 收藏0
  • Spring Boot 应用 Docker 化 《Spring Boot 2.0极简教程》(陈光剑)

    摘要:应用化极简教程陈光剑基于的企业级应用开发最佳实践前面的章节中,我们都是在环境中开发运行测试应用程序。关键字是分布式应用微服务容器虚拟化。通常,在企业项目实践中,会实现一套应用部署发布的自动化运维平台工具。 Spring Boot 应用 Docker 化 《Spring Boot 2.0极简教程》(陈光剑)—— 基于 Gradle + Kotlin的企业级应用开发最佳实践 前面的章节中,...

    rose 评论0 收藏0
  • Spring Boot 2 - 使用CommandLineRunner与ApplicationRun

    摘要:命令行参数传递之前我们说过使用的一大优势就是可以将工程直接打包成一个包而不需要单独部署。执行获取到参数执行结果我们可以发现,通过方法的参数可以很方便地获取到命令行参数的值。如果需要获取命令行参数时则建议使用。 本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用。 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本...

    alogy 评论0 收藏0
  • Spring-Boot学习笔记

    摘要:学习笔记使用很容易创建一个独立运行运行内嵌容器准生产级别的基于框架的项目,使用你可以不用或者只需要很少的配置。异常消息如果这个错误是由异常引起的。错误发生时请求的路径。 Spring-Boot 1.5 学习笔记 使用Spring Boot很容易创建一个独立运行(运行jar,内嵌Servlet容器)、准生产级别的基于Spring框架的项目,使用Spring Boot你可以不用或者只需要很...

    curlyCheng 评论0 收藏0
  • Spring Boot 参考指南(安装 Spring Boot

    摘要:安装可以与经典开发工具一起使用,也可以作为命令行工具安装。下面的示例展示了一个典型的文件安装命令行接口是一个命令行工具,你可以使用它来快速地实现的原型。 10. 安装Spring Boot Spring Boot可以与经典Java开发工具一起使用,也可以作为命令行工具安装。无论哪种方式,都需要Java SDK v1.8或更高版本。在开始之前,你应该使用以下命令检查当前的Java安装: ...

    Donald 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<