springboot的CommandLineRunner和ApplicationRunner

前言:

Spring Boot如何解决项目启动时初始化资源,在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。

为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring boot会自动监测到它们。这两个接口都有一个run()方法,在实现接口时需要覆盖该方法,并使用@Component注解使其成为bean。

CommandLineRunner和ApplicationRunner的作用是相同的。不同之处在于CommandLineRunner接口的run()方法接收String数组作为参数,即是最原始的参数,没有做任何处理;而ApplicationRunner接口的run()方法接收ApplicationArguments对象作为参数,是对原始参数做了进一步的封装。

当程序启动时,我们传给main()方法的参数可以被实现CommandLineRunner和ApplicationRunner接口的类的run()方法访问,即可接收启动服务时传过来的参数。我们可以创建多个实现CommandLineRunner和ApplicationRunner接口的类。为了使他们按一定顺序执行,可以使用@Order注解,order数字越小优先级越高

默认情况下ApplicationRunner优先于CommandLineRunner执行

使用

ApplicationRunner

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;

//必须交给spring管理
@Component
public class Runner1 implements ApplicationRunner {
	
	/**
     * @param args 此处的项目启动参数是经过处理的
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
    	//打印启动参数
        System.out.println("-------------Runner1-------------");
        List<String> nonOptionArgs = args.getNonOptionArgs();
        System.out.println("nonOptionArgs = " + nonOptionArgs);
        String[] sourceArgs = args.getSourceArgs();
        System.out.println("Arrays.toString(sourceArgs) = " + Arrays.toString(sourceArgs));
        Set<String> optionNames = args.getOptionNames();
        System.out.println("optionNames = " + optionNames);
        List<String> name = args.getOptionValues("name");
        System.out.println("name = " + name);	
        //调用业务接口,完成初始化操作
        TestService testService = SpringContextUtils.getBean(TestService.class);
        testService.init();
    }
}

CommandLineRunner

import com.zyp.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class Line implements CommandLineRunner {

    /**
     * 注入业务接口
     */
    @Autowired
    private TestService service;

    /**
     * @param args 原始的项目启动配置参数
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
    	//打印启动参数
        System.out.println("Arrays.toString(args) = " + Arrays.toString(args));
        //调用业务逻辑
        service.init();
    }
}

项目启动参数配置

新建配置

新建配置

配置项目信息

请添加图片描述

相关类

SpringContextUtils

package com.zyp.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author leishen
 */
@Component
public class SpringContextUtils implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext=applicationContext;
    }

    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

    public static <T> T getBean(String name, Class<T> requiredType) {
        return applicationContext.getBean(name, requiredType);
    }

    public static boolean containsBean(String name) {
        return applicationContext.containsBean(name);
    }

    public static boolean isSingleton(String name) {
        return applicationContext.isSingleton(name);
    }

    public static Class<? extends Object> getType(String name) {
        return applicationContext.getType(name);
    }
}

TestService

package com.zyp.service;

public interface TestService {

    void init();
}

TestServiceImpl

package com.zyp.service.impl;

import com.zyp.service.TestService;
import org.springframework.stereotype.Service;

/**
 * @author leishen
 */
@Service
public class TestServiceImpl implements TestService {

    @Override
    public void init() {
        System.out.println("hello world");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值