SpringBoot核心特性——ApplicationRunner && CommandLineRunner使用

46 篇文章 3 订阅
7 篇文章 0 订阅

前言

如果想在SpringApplication启动后做一些操作,那么除了可以监听ApplicationReadyEvent事件外,还可以实现ApplicationRunner或CommandLineRunner接口.

ApplicationRunner

新建一个ApplicationRunner实现,代码如下:

java复制代码package geek.springboot.application.runner;  
  
import lombok.extern.slf4j.Slf4j;  
import org.springframework.boot.ApplicationArguments;  
import org.springframework.boot.ApplicationRunner;  
import org.springframework.stereotype.Component;  
  
/**  
* 自定义{@link ApplicationRunner}实现  
*  
* @author Bruse  
*/
@Slf4j  
@Component  
public class CustomApplicationRunner implements ApplicationRunner {  
  
    /**  
    * 在SpringApplication启动完成后,该方法会被回调,可以在这里做一些操作  
    *  
    * @param args 启动Java Application时设置的程序参数  
    * @throws Exception 异常  
    */  
    @Override  
    public void run(ApplicationArguments args) throws Exception {  
        // 简单打印一下应用参数
        log.info("arguments is {}", Arrays.toString(args.getSourceArgs()));
    }  
  
}

这里ApplicationRunner的run()方法实现,仅仅打印一下应用参数,所以在启动Java Application前,先在配置一些应用参数【笔者这里用到的IDEA版本为2023.1.4】:

image.png

image.png

image.png

启动SpringApplication,输出如下:

image.png

可以看到CustomApplicationRunner的run()方法在SpringApplicaiton启动后被回调,并且输出我们配置的应用参数.

CommandLineRunner

新建一个CommandLineRunner实现,代码如下:

java复制代码package geek.springboot.application.runner;  
  
import lombok.extern.slf4j.Slf4j;  
import org.springframework.boot.CommandLineRunner;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
  
  
/**  
* 自定义{@link CommandLineRunner}实现  
*/  
@Slf4j  
@Component  
public class CustomCommandLineRunner implements CommandLineRunner {  
  
    /**  
    * 在SpringApplication启动完成后,该方法会被回调,可以在这里做一些操作  
    *  
    * @param args 启动Java Application时设置的程序参数  
    * @throws Exception 异常  
    */  
    @Override  
    public void run(String... args) throws Exception {  
        // 这里简单做一下参数打印
        log.info("args is {}", Arrays.toString(args));  
    }  
  
}

启动SpringApplication,控制台成功输出:

image.png

两者的区别

ApplicationRunner和CommandLineRunner两个接口唯一的区别在于,两者run()方法的参数不一样,虽然参数都是Java应用启动时设置的参数,但ApplicationRunner将参数包装成了ApplicationArguments,CommandLineRunner则没有多过包装,直接就是传递一个String[]给实现者进行参数读取.

image.png

image.png

优先级设置

如果必须按特定顺序调用ApplicationRunner或CommandLineRunner的run()方法的话,可以使用Order注解或实现Ordered接口.

对CustomApplicationRunner稍作调整,让其实现Ordered接口,改动后代码如下:

java复制代码package geek.springboot.application.runner;  
  
import lombok.extern.slf4j.Slf4j;  
import org.springframework.boot.ApplicationArguments;  
import org.springframework.boot.ApplicationRunner;  
import org.springframework.core.Ordered;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
  
/**  
* 自定义{@link ApplicationRunner}实现  
*  
* @author Bruse  
*/  
@Slf4j  
@Component  
public class CustomApplicationRunner implements ApplicationRunner, Ordered {  
  
    /**  
    * 在SpringApplication启动完成后,该方法会被回调,可以在这里做一些操作  
    *  
    * @param args 启动Java Application时设置的程序参数  
    * @throws Exception 异常  
    */  
    @Override  
    public void run(ApplicationArguments args) throws Exception {  
        log.info("arguments is {}", Arrays.toString(args.getSourceArgs()));  
    }  
  
    @Override  
    public int getOrder() {  
        // 返回优先级,数值越小,优先级越高  
        return 0;  
    }  
}

对CustomCommandLineRunner稍作调整,给其加上@Order注解,代码如下:

java复制代码package geek.springboot.application.runner;  
  
import lombok.extern.slf4j.Slf4j;  
import org.springframework.boot.CommandLineRunner;  
import org.springframework.core.annotation.Order;  
import org.springframework.stereotype.Component;  
  
import java.util.Arrays;  
  
  
/**  
* 自定义{@link CommandLineRunner}实现  
*/  
@Slf4j  
@Order(-1) // 数值越小,优先级越高  
@Component  
public class CustomCommandLineRunner implements CommandLineRunner {  
  
    /**  
    * 在SpringApplication启动完成后,该方法会被回调,可以在这里做一些操作  
    *  
    * @param args 启动Java Application时设置的程序参数  
    * @throws Exception 异常  
    */  
    @Override  
    public void run(String... args) throws Exception {  
        log.info("args is {}", Arrays.toString(args));  
    }  
  
}

CustomCommandLineRunner定义的优先级比CustomApplicationRunner高,也就是CustomCommandLineRunner的run()永远优于CustomApplicationRunner的run(),控制台输出如下:

image.png

注意不要做耗时的任务

本质上启动SpringApplication的线程,和回调ApplicationRunner、CommandLineRunner的run()方法的线程是同一个,所以注意不要在run()中执行过于复杂耗时的任务.

源码分析

查看SpringApplication的run()方法,关键代码如下:

image.png

查看callRunners()方法,可以看到逻辑非常简单,就是获取所有ApplicationRunner和CommandLineRunner的实现,然后做一个优先级排序,最后回调run()方法.

image.png

image.png

而且从源码中还可以看出,ApplicationRunner和CommandLineRunner的run()回调,是在ApplicationReadyEvent事件广播之前,核心代码如下:

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在SpringBoot中实现跳转到静态HTML页面,可以使用控制器(Controller)的方式来实现。具体的实现步骤如下: 1. 在SpringBoot项目的resources文件夹下创建一个static文件夹,用来存放静态HTML文件。 2. 在控制器中添加一个请求映射(RequestMapping)注解,并指定需要跳转的静态HTML页面路径。 3. 在方法体中使用ModelAndView返回静态HTML页面的名称。 示例代码如下: ```java @Controller public class HtmlController { @RequestMapping("/index") public ModelAndView index() { ModelAndView modelAndView = new ModelAndView(); modelAndView.setViewName("index.html"); return modelAndView; } } ``` 在上面的代码中,我们定义了一个控制器类HtmlController,并在其中添加了一个请求映射注解@RequestMapping("/index"),表示当访问http://localhost:8080/index时,将会执行index()方法。 在index()方法中,我们使用ModelAndView类来指定需要跳转的静态HTML页面名称,即modelAndView.setViewName("index.html")。 最后返回ModelAndView对象即可。 需要注意的是,SpringBoot默认情况下会将静态文件放置在classpath下的static目录中,如果需要更改静态文件的存放位置,可以在application.properties中添加以下配置: ```properties # 指定静态文件存放位置 spring.resources.static-locations=classpath:/static/,file:/usr/local/static/ ``` 上面的配置表示将静态文件放置在classpath下的static目录和本地磁盘的/usr/local/static/目录下。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值