Spring Boot中的系统启动任务


在 Servlet/Jsp 项目中,如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进行,以后都不再执行,这里,容易想到web基础中的三大组件( Servlet、Filter、Listener )之一 Listener ,这种情况下,一般定义一个 ServletContextListener,然后就可以监听到项目启动和销毁,进而做出相应的数据初始化和销毁操作,例如:

public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //数据初始化操作
    }
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //数据销毁操作
    }
}

这是基础 web 项目的解决方案,使用了 Spring Boot,那么我们可以使用更为简便的方式。Spring Boot 中针对系统启动任务提供了两种解决方案,分别是 CommandLineRunner 和 ApplicationRunner,下面我们就俩看看具体的实现

通过CommandLineRunner 实现系统启动任务

我们需要自定义MyCommandLineRunner ,实现CommandLineRunner ,实现默认的run方法

package com.zhouym.systemstartuptask;

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

import java.util.Arrays;

/**
 * 〈〉
 *
 * @author zhouym
 * @create 2019/8/9
 * @since 1.0.0
 */
@Component
@Order(100)
public class MyCommandLineRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("MyCommandLineRunner:" + Arrays.toString(args));

    }
}

1、通过@Component注解将MyCommandLineRunner 注册为spring容器中的一个bean
2、通过@Order注解实现启动任务的优先级,默认是Integer.MAX_VALUE,这里面的数字越大,优先级就越低
3、在重写的run方法中处理逻辑代码,在系统启动就会执行我们的run方法
4、run方法中的参数来自我们项目的启动参数,即项目的入口处
参数传递有两种方式,一种是就是下图中,可以在Program Arguments中设置参数
在这里插入图片描述
还有一种是现将项目打包,然后在在termianl中
在这里插入图片描述

cd target

然后再执行java -jar devtools-0.0.1-SNAPSHOT.jar 参数名(可以传递多个)

通过ApplicationRunner 实现系统启动任务

ApplicationRunner 和 CommandLineRunner 功能一致,用法也基本一致,唯一的区别主要体现在对参数的处理上,ApplicationRunner 可以接收更多类型的参数(ApplicationRunner 除了可以接收 CommandLineRunner 的参数之外,还可以接收 key/value形式的参数)。

使用 ApplicationRunner ,自定义类实现 ApplicationRunner 接口即可,组件注册以及组件优先级的配置都和 CommandLineRunner 一致,如下:

package com.zhouym.systemstartuptask;

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 zhouym
 * @create 2019/8/9
 * @since 1.0.0
 */
@Component
@Order(100)
public class MyApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        //获取命令行中无key参数
        List<String> nonOptionArgs = args.getNonOptionArgs();
        //获取所有key/value形式的参数的key
        Set<String> optionNames = args.getOptionNames();
        for (String key:
             optionNames) {
            //根据key获取key/value形式的参数的value值
            System.out.println("MyApplicationRunner---"+key+":"+args.getOptionValues(key));
        }
        //获取命令行中的所有参数
        String[] sourceArgs = args.getSourceArgs();
        System.out.println("MyApplicationRunner:"+ Arrays.toString(sourceArgs));
    }
}

ApplicationRunner 定义完成后,传启动参数也是两种方式,参数类型也有两种,第一种和 CommandLineRunner 一致,第二种则是 –key=value 的形式,在 IDEA 中定义方式如下:
在这里插入图片描述
运行启动类,我们来看看
在这里插入图片描述

发布了207 篇原创文章 · 获赞 87 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/zhouym_/article/details/98989488