SpringBoot的ApplicationRunner

springboot~ EventListener事件监听的使用
https://www.cnblogs.com/lori/p/9871362.html

SpringBoot的ApplicationRunner
执行时机为容器启动完成的时候。
https://blog.csdn.net/jdd92/article/details/81053404

——————————————————————————————————————————————————

 

在开发中可能会有这样的情景。需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了两个接口来帮助我们实现这种需求。这两个接口分别为CommandLineRunner和ApplicationRunner。他们的执行时机为容器启动完成的时候。

这两个接口中有一个run方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。目前我在项目中用的是ApplicationRunner。是这么实现的:

package com.jdddemo.demo.controller;

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

@Component
public class JDDRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(args);
System.out.println("这个是测试ApplicationRunner接口");
}
}

 

———————————————————————————————————————————————————————————

读取参数?

在我们开始快速说明之前。在这两种情况下,无论是CommandLineRunner还是ApplicationRunner,都始终支持Spring的属性处理。我们可以像往常一样使用@Value注释注入值。

完整的工作源代码在这里

首先,我们创建一个简单的Spring Boot应用程序并实现ApplicationRunner接口。现在,Spring可以获得我们的这个类并执行它。它类似于CommandLineRunner。

我们还必须实现一个方法run,但是我们将ApplicationArguments 作为入参数而不是String列表。

ApplicationArguments区分选项参数和非选项参数。选项参数是我们可以通过Spring Boot属性处理使用的(如 app.name = Myapp)。它们还可以通过传入逗号分隔列表或多次使用参数来为每个选项包含多个值。

非选项参数是我们在命令行传递除了VM参数的所有其他参数。

在示例中,我们将打印出我们收到的每种类型的参数,然后显示它们。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@SpringBootApplication
public class CommandlineAppApplication implements ApplicationRunner{
  public static void main(String[] args) {
    SpringApplication.run(CommandlineAppApplication.class, args);
  }
  @Override
  public void run(ApplicationArguments args) throws Exception {
    System.out.println("# NonOptionArgs: " + args.getNonOptionArgs().size());
    System.out.println("NonOptionArgs:");
    args.getNonOptionArgs().forEach(System.out::println);
    System.out.println("# OptionArgs: " + args.getOptionNames().size());
    System.out.println("OptionArgs:");
    args.getOptionNames().forEach(optionName -> {
      System.out.println(optionName + "=" + args.getOptionValues(optionName));
    });
  }
}

非选项参数通过getNonOptionArgs()作为字符串列表重新获得。

而对于选项参数,我们可以通过接收选项名称getOptionNames和实际值通过getOptionValues,它会返回一个列表字符串。

当我们现在启动应用程序并传递一些参数,如:

1
java -jar commandline-app-0.0.1-SNAPSHOT.jar iamnonoption --app.name=CmdRulez --app.hosts=abc,def,ghi --app.name=2

上述执行的输出:

#NonOptionArgs:1个
NonOptionArgs:
nonoption
#OptionArgs:2个
OptionArgs:
app.hosts = [ABC,DEF,GHI]
app.name = [CmdRulez,2]

 

——————————————————————————————————————————————————————————

 spring boot:ApplicationRunner和CommandLineRunner用法区别 

业务场景:
应用服务启动时,加载一些数据和执行一些应用的初始化动作。如:删除临时文件,清除缓存信息,读取配置文件信息,数据库连接等。
1、SpringBoot提供了CommandLineRunner和ApplicationRunner接口。当接口有多个实现类时,提供了@order注解实现自定义执行顺序,也可以实现Ordered接口来自定义顺序。
注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。
两者的区别在于:
ApplicationRunner中run方法的参数为ApplicationArguments,而CommandLineRunner接口中run方法的参数为String数组。想要更详细地获取命令行参数,那就使用ApplicationRunner接口

ApplicationRunner

@Component
@Order(value = 10)
public class AgentApplicationRun2 implements ApplicationRunner {
	@Override
	public void run(ApplicationArguments applicationArguments) throws Exception {

	}
}

 

CommandLineRunner

@Component
@Order(value = 11)
public class AgentApplicationRun implements CommandLineRunner {

	@Override
	public void run(String... strings) throws Exception {

	}
}
posted @ 2019-09-10 11:24  kelelipeng  阅读(1436)  评论(0编辑  收藏  举报