SpringBoot中的ApplicationRunner与CommandLineRunner问题

目录
  • 概述
  • 实现启动加载接口
    • ApplicationRunner接口的示例
    • CommandLineRunner接口示例
  • CommandLineRunner和ApplicationRunner的执行顺序
    • 我们使用@Order注解按顺序执行这四个bean

概述

开发中可能会有这样的场景,需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。

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
@Order(value = 1)
public class JDDRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println(args);
        System.out.println("这个是测试ApplicationRunner接口");
    }
}

执行结果如下:

CommandLineRunner接口示例

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class TestCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("<这个是测试CommandLineRunn接口");
    }
}

CommandLineRunner和ApplicationRunner的执行顺序

在spring boot程序中,我们可以使用不止一个实现CommandLineRunner和ApplicationRunner的bean。

为了有序执行这些bean的run()方法,可以使用@Order注解或Ordered接口。

下面例子中创建了两个实现CommandLineRunner接口bean和两个实现ApplicationRunner接口的bean。

我们使用@Order注解按顺序执行这四个bean

CommandLineRunnerBean1.java

@Component
@Order(1)
public class CommandLineRunnerBean1 implements CommandLineRunner {
    @Override
    public void run(String... args) {
        System.out.println("CommandLineRunnerBean 1");
    }
}

ApplicationRunnerBean1.java

@Component
@Order(2)
public class ApplicationRunnerBean1 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments arg0) throws Exception {
        System.out.println("ApplicationRunnerBean 1");
    }
}

CommandLineRunnerBean2.java

@Component
@Order(3)
public class CommandLineRunnerBean2 implements CommandLineRunner {
    @Override
    public void run(String... args) {
        System.out.println("CommandLineRunnerBean 2");
    }
}

ApplicationRunnerBean2.java

@Component
@Order(4)
public class ApplicationRunnerBean2 implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments arg0) throws Exception {
        System.out.println("ApplicationRunnerBean 2");
    }
}

输出结果为:

CommandLineRunnerBean 1
ApplicationRunnerBean 1
CommandLineRunnerBean 2
ApplicationRunnerBean 2

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • springboot CommandLineRunner接口实现自动任务加载功能

    CommandLineRunner接口可以实现任务的自动加载,当项目启动完后,就会自动去执行CommandLineRunner接口里的run方法,你可以实现多个CommandLineRunner的实例,使用order来控制执行的顺序! /** * 项目启动后自动运行的代码CommandLineRunner */ @Component @Order(1) public class MyStartupRunner1 implements CommandLineRunner { private Log

  • 使用SpringBoot的CommandLineRunner遇到的坑及解决

    目录 使用场景 两个接口的不同 特殊的场景 遇到的坑 填坑 总结 使用场景 再应用程序开发过程中,往往我们需要在容器启动的时候执行一些操作. Spring Boot中提供了CommandLineRunner和ApplicationRunner两个接口来实现这样的需求. 两个接口的不同 参数不同,其他大体相同,可根据实际需求选择合适的接口使用. CommandLineRunner接口中run方法的参数为String数组,ApplicationRunner中run方法的参数为ApplicationA

  • springboot使用CommandLineRunner解决项目启动时初始化资源的操作

    前言: 在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等. 今天就给大家介绍一个 Spring Boot 神器,专门帮助大家解决项目启动初始化资源操作. 这个神器就是 CommandLineRunner,CommandLineRunner 接口的 Component 会在所有 Spring Beans 都初始化之后,SpringApplication.run() 之前执行,非常适合在应用程序启动之初进行一些数据初始化的工作. 正文

  • SpringBoot使用CommandLineRunner接口完成资源初始化方式

    目录 1 简介 1.1 应用场景 1.2 CommandLineRunner接口 1.3 ApplicationRunner接口 1.4 @Order注解 1.5 CommandLineRunner和ApplicationRunner区别 2 CommandLineRunner完成资源初始化 2.1 背景介绍 2.2 数据类型关系 2.3 实现过程 2.4 源码实现 3 总结 1 简介 1.1 应用场景 在应用服务启动时,需要在所有Bean生成之后,加载一些数据和执行一些应用的初始化. 例如:删

  • SpringBoot中实现启动任务的实现步骤

    我们在项目中会用到项目启动任务,即项目在启动的时候需要做的一些事,例如:数据初始化.获取第三方数据等等,那么如何在SpringBoot 中实现启动任务,一起来看看吧 SpringBoot 中提供了两种项目启动方案,CommandLineRunner 和 ApplicationRunner 一.CommandLineRunner 使用 CommandLineRunner ,需要自定义一个类区实现 CommandLineRunner 接口,例如: import org.springframework

  • 浅谈SpringBoot中的Bean初始化方法 @PostConstruct

    目录 注解说明 代码示例 注解示例 错误示例 正确示例 SpringBoot @PostConstruct虽好,也要慎用 1 问题的产生 2 案例模拟 3 总结 注解说明 使用注解: @PostConstruct 效果:在Bean初始化之后(构造方法和@Autowired之后)执行指定操作.经常用在将构造方法中的动作延迟. 备注:Bean初始化时候的执行顺序: 构造方法 -> @Autowired -> @PostConstruct 代码示例 注解示例 @Component public cl

  • springboot中如何实现kafa指定offset消费

    这篇文章主要介绍了springboot中如何实现kafa指定offset消费,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 kafka消费过程难免会遇到需要重新消费的场景,例如我们消费到kafka数据之后需要进行存库操作,若某一时刻数据库down了,导致kafka消费的数据无法入库,为了弥补数据库down期间的数据损失,有一种做法我们可以指定kafka消费者的offset到之前某一时间的数值,然后重新进行消费. 首先创建kafka消费服务 @S

  • 在SpringBoot中整合使用Netty框架的详细教程

    Netty是一个非常优秀的Socket框架.如果需要在SpringBoot开发的app中,提供Socket服务,那么Netty是不错的选择. Netty与SpringBoot的整合,我想无非就是要整合几个地方 让netty跟springboot生命周期保持一致,同生共死 让netty能用上ioc中的Bean 让netty能读取到全局的配置 整合Netty,提供WebSocket服务 这里演示一个案例,在SpringBoot中使用Netty提供一个Websocket服务. servlet容器本身提

  • 如何正确控制springboot中bean的加载顺序小结篇

    1.为什么需要控制加载顺序 springboot遵从约定大于配置的原则,极大程度的解决了配置繁琐的问题.在此基础上,又提供了spi机制,用spring.factories可以完成一个小组件的自动装配功能. 在一般业务场景,可能你不大关心一个bean是如何被注册进spring容器的.只需要把需要注册进容器的bean声明为@Component即可,spring会自动扫描到这个Bean完成初始化并加载到spring上下文容器. 而当你在项目启动时需要提前做一个业务的初始化工作时,或者你正在开发某个中间

  • SpringBoot中使用Quartz管理定时任务的方法

    定时任务在系统中用到的地方很多,例如每晚凌晨的数据备份,每小时获取第三方平台的 Token 信息等等,之前我们都是在项目中规定这个定时任务什么时候启动,到时间了便会自己启动,那么我们想要停止这个定时任务的时候,就需要去改动代码,还得启停服务器,这是非常不友好的事情 直至遇见 Quartz,利用图形界面可视化管理定时任务,使得我们对定时任务的管理更加方便,快捷 一.Quartz 简介 Quartz是一个开源的作业调度框架,它完全由Java写成,并设计用于J2SE和J2EE应用中.它提供了巨大的灵

  • 浅谈在springboot中使用定时任务的方式

    springboot定时任务 在springboot环境下有多种方法,这里记录下使用过的其中两种:1.使用注解,2.通过实现接口的方式. 使用注解的方式虽然比较简单,但是如果项目需要用户对定时周期进行修改操作,只使用注解就比较难实现.所以可以使用实现接口的方式.通过对接口的实现,可以在项目运行时根据需要修改任务执行周期,只需要关闭原任务再开启新任务即可. 1.使用注解方式 ​ 首先需要在启动类下添加 @EnableScheduling 注解(@EnableAsync是开启异步的注解) packa

  • springboot中项目启动时实现初始化方法加载参数

    目录 springboot项目启动,初始化方法加载参数 1.@PostConstruct说明 2.@PreDestroy说明 第一种:注解@PostConstruct 第二种:实现CommandLineRunner接口 第三种:springboot的启动类 springboot初始化参数顺序 spring初始化参数顺序为 springboot项目启动,初始化方法加载参数 今天我看到项目中用到了 @PostConstruct 这个注解,之前没看到过,特地查了一下, 1.@PostConstruct

  • Springboot中的@Order如何使用

    在spring-boot 2.6.2下测试,@Order并不会影响bean的装载顺序,声明了@Component的类,无论是构造方法.@PostConstruct注解声明的方法,还是实现的InitializingBean接口中的afterPropertiesSet()方法,如果beanClass位于同样的目录层级,这些方法的调用只会受到className的顺序影响: @Component @Slf4j @Order(2) public class Bean1 implements Initial

  • 详解SpringBoot中实现依赖注入功能

    今天给大家介绍一下SpringBoot中是如何实现依赖注入的功能. 在以往spring使用中,依赖注入一般都是通过在Spring的配置文件中添加bean方法实现的,相对于这个方式SpringBoot的实现方式就显得非常便捷了.SpringBoot的实现方式基本都是通过注解实现的. 下面来看一下具体案例,这里我编写了三个测试类用于测试依赖注入到底是否可以正确实现. TestBiz接口: package example.biz; public interface TestBiz { public S

随机推荐