avatar

SpringBoot中实现系统启动任务

摘要:本文介绍了如何通过CommanLineRunner和ApplicationRunner来实现系统启动任务。

CommanLineRunner


我们来创建自定义的MyCommanLineRunner类,并实现CommanLineRunner接口即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.commandlinerunner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
* @author: WJZheng
* @date: 2020/3/17 11:19
* @description:
*/

@Component
@Order(99) //如果有多个CommanLineRunner,则需要指定优先级,数字越小优先级越高
public class MyCommandLineRunner1 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
//run方法里面的args参数就是SpringBoot主类中main方法传递的args
System.out.println(this.toString() + Arrays.toString(args));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.commandlinerunner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
* @author: WJZheng
* @date: 2020/3/17 11:19
* @description:
*/

@Component
@Order(100)
public class MyCommandLineRunner2 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println(this.toString() + Arrays.toString(args));
}
}

我们在Terminal中运行命令

1
mvn package

将项目打包成jar包,然后运行

1
java -jar commandlinerunner-0.0.1-SNAPSHOT.jar ThreeBody BallLightning ChineseSun

通过命令行的方式传递args参数,在控制台我们可以看到以下信息,即系统启动的时候会自动运行两个CommanLineRunner类的run方法。

ApplicatinoRunner


我们再通过ApplicationRunner来实现系统启动,与上面一样创建自定义类,并实现ApplicationRunner接口即可。我们同样也可以通过@Order来设置其优先级。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.example.commandlinerunner;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
* @author: WJZheng
* @date: 2020/3/17 11:37
* @description:
*/
@Component
@Order(99)
public class MyApplicationRunner1 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
//获取启动的所有参数
String[] sourceArgs = args.getSourceArgs();
System.out.println("sourceArgs: " + Arrays.toString(sourceArgs));
List<String> noOptionArgs = args.getNonOptionArgs();
System.out.println("nonOptionArgs: " + noOptionArgs);
Set<String> optionNames = args.getOptionNames();
System.out.println("--------------------------");
for (String name : optionNames) {
System.out.println(name + ": " + args.getOptionValues(name));
}
System.out.println("End: " + this.toString());
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.example.commandlinerunner;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
* @author: WJZheng
* @date: 2020/3/17 11:37
* @description:
*/
@Component
@Order(100)
public class MyApplicationRunner2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
//获取启动的所有参数
String[] sourceArgs = args.getSourceArgs();
System.out.println("sourceArgs: " + Arrays.toString(sourceArgs));
//ApplicationRunner可以获取两种不同的参数,一种是noOptionArgs,一种是OptionArgs
List<String> noOptionArgs = args.getNonOptionArgs();
System.out.println("nonOptionArgs: " + noOptionArgs);
Set<String> optionNames = args.getOptionNames();
System.out.println("--------------------------");
for (String name : optionNames) {
System.out.println(name + ": " + args.getOptionValues(name));
}
System.out.println("End: " + this.toString());
}
}

同时CommanLineRunner与ApplicationRunner不冲突,两者可以共存,ApplicationRunner优先级高于CommanLineRunner。我们来运行验证一下,同样先打包成jar包,然后在Terminal运行

1
java -jar commandlinerunner-0.0.1-SNAPSHOT.jar ThreeBody BallLightning ChineseSun

我们再来看看若设置参数名称会怎么样,在Terminal运行

1
java -jar commandlinerunner-0.0.1-SNAPSHOT.jar --name=ThreeBody --name=BallLightning --name=ChineseSun --author=LiuCiXin Spring SpringBoot SpringMVC

我们可以看到,ApplicationRunner可以识别OptionArgs,但CommanLineRunner不能识别,而将其全部视为noOptionArgs

Author: WJZheng
Link: https://wellenzheng.github.io/2020/03/23/SpringBoot%E4%B8%AD%E5%AE%9E%E7%8E%B0%E7%B3%BB%E7%BB%9F%E5%90%AF%E5%8A%A8%E4%BB%BB%E5%8A%A1/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.

Comment