springboot实现系统启动任务
1.什么是系统启动任务
系统启动任务就是当项目启动的时候能够执行的东西就是系统启动任务,即随着项目启动而执行的。如果是ssm
项目一般可以通过自定义listener
监听器实现,监听servletContext
的启动然后做一些初始化的操作;springboot
中也可以这么做,但是也有自己的方案即CommandLineRunner
和ApplicationRunner
来实现。
2.CommandLineRunner实现系统启动任务
首先自定一个类实现CommandLineRunner
接口,然后重写里面的run
方法,这个run
方法里面的参数就是启动类main
方法上的参数,默认是为空的。
下面demo
中有两个类都是实现了CommandLineRunner
接口,存在多个系统启动任务时就有一个优先级的问题,可以使用@Order
注解指定优先级,数字越小优先级越大。
package com.cls.commandlinerunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* @author by CLS
* @Classname MyCommandLineRunner
* @Description TODO
* @Date 2021/5/15/0015 9:06:46
*/
@Component
@Order(100)
public class MyCommandLineRunner implements CommandLineRunner {
/**
* 当系统启动时,run方法会被触发,方法参数就是main方法所传入的参数
* 如果存在多个系统启动任务的话,必然存在一个优先级的问题,可以使用@Order注解
* 指定优先级,数字越小优先级越大
* @param args 这个参数就是系统启动类上的args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
//默认打印出来是空的
System.out.println("args = " + Arrays.toString(args));
}
}
package com.cls.commandlinerunner;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.Arrays;
/**
* @author by CLS
* @Classname MyCommandLineRunner
* @Description TODO
* @Date 2021/5/15/0015 9:06:46
*/
@Component
@Order(90)
public class MyCommandLineRunner2 implements CommandLineRunner {
/**
* 当系统启动时,run方法会被触发,方法参数就是main方法所传入的参数
* @param args 这个参数就是系统启动类上的args
* @throws Exception
*/
@Override
public void run(String... args) throws Exception {
System.out.println("args2 = " + Arrays.toString(args));
}
}
结果:
- 默认的参数都问空,怎么指定参数呢?
1.在idea
中指定参数:
2.项目打包之后指定参数:
3.ApplicationRunner实现系统启动任务
package com.cls.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 by CLS
* @Classname MyApplicationRunner
* @Description TODO
* @Date 2021/5/15/0015 9:30:07
*/
@Component
@Order(70)
public class MyApplicationRunner2 implements ApplicationRunner {
/**
*
* @param args
* @throws Exception
*/
@Override
public void run(ApplicationArguments args) throws Exception {
//获取没有键的参数,获取到的值和 commandlinerunner 一致
List<String> nonOptionArgs = args.getNonOptionArgs();
System.out.println("nonOptionArgs2 = " + nonOptionArgs);
//获取所有的键
Set<String> optionNames = args.getOptionNames();
System.out.println("optionNames = " + optionNames);
for (String optionName : optionNames) {
System.out.println("optionName2: " + optionName +"---"+ args.getOptionValues(optionName));
}
//获取命令行中的所有参数,不管有没有键的
String[] sourceArgs = args.getSourceArgs();
System.out.println("sourceArgs2 = " + Arrays.toString(sourceArgs));
}
}
- 总结:
ApplicationRunner
和CommandLineRunner
这两个实现系统启动任务都差不多,只是run
方法的参数不同,以及获取参数值的方式不同