ApplicationRunner 类说明
在开发中可能会有这样的情景。需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了两个接口来帮助我们实现这种需求。这两个接口分别为 CommandLineRunner和 ApplicationRunner。他们的执行时机为容器启动完成的时候。
这两个接口中有一个 run方法,我们只需要实现这个方法即可。这两个接口的不同之处在于:ApplicationRunner中 run方法的参数为 ApplicationArguments,而CommandLineRunner接口中 run方法的参数为 String数组。目前我在项目中用的是ApplicationRunner。是这么实现的:args 是系统启动时传入的参数。
1 package com.zzx.service.start;
2
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 import org.springframework.beans.factory.annotation.Autowired;
6 import org.springframework.boot.ApplicationArguments;
7 import org.springframework.boot.ApplicationRunner;
8 import org.springframework.stereotype.Component;
9
10 /**
11 * @ClassName: BootStart
12 * @Description: 系统入口
13 * @Author: zzx
14 * @Date: 2020/9/22 17:26
15 */
16 @Component
17 public class BootStart implements ApplicationRunner {
18 private static final Logger logger = LoggerFactory.getLogger(BootStart.class);
19
20
21 @Override
22 public void run(ApplicationArguments args) {
23 try {
24 logger.info("启动系统线程...");
25
26 //创建线程池
27 ThreadFactory threadFactory = Executors.defaultThreadFactory();
28 ExecutorService executorService = new ThreadPoolExecutor(1,1,0L,
29 TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>(),threadFactory);
30
31 //(() -> {}) lambda表达式
32 executorService.execute(() -> {
33
34 });
35 } catch (Exception e) {
36 logger.error("******初始化系统异常:", e);
37 }
38 }
39 }
ApplicationRunner 类说明的更多相关文章
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- springboot备忘
1.springboot中有ApplicationRunner类,如果项目中的启动类名称也是ApplicationRunner,单元测试时需要注意:import不要import到springboot的 ...
- 源码分析SpringBoot启动
遇到一个问题,需要从yml文件中读取数据初始化到static的类中.搜索需要实现ApplicationRunner,并在其实现类中把值读出来再set进去.于是乎就想探究一下SpringBoot启动中都 ...
- SpringBoot启动加载类ApplicationRunner
SpringBoot启动加载类ApplicationRunner 有时希望项目在启动的时候加载一些系统参数,就要用到ApplicationRunner ApplicationRunner是一个接口,我 ...
- Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner
本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...
- 使用CommandLineRunner或ApplicationRunner接口创建bean
在spring boot应用中,我们可以在程序启动之前执行任何任务.为了达到这个目的,我们需要使用CommandLineRunner或ApplicationRunner接口创建bean,spring ...
- Spring Boot的SpringApplication类详解
相信使用过Spring Boot的开发人员,都对Spring Boot的核心模块中提供的SpringApplication类不陌生.SpringApplication类的run()方法往往在Sprin ...
- springboot项目启动之后初始化自定义配置类
前言 今天在写项目的时候,需要再springboot项目启动之后,加载我自定义的配置类的一些方法,百度了之后特此记录下. 正文 方法有两种: 1. 创建自定义类实现 CommandLineRunner ...
- SpringBoot源码分析之---SpringBoot项目启动类SpringApplication浅析
源码版本说明 本文源码采用版本为SpringBoot 2.1.0BUILD,对应的SpringFramework 5.1.0.RC1 注意:本文只是从整体上梳理流程,不做具体深入分析 SpringBo ...
- SpringBoot的ApplicationRunner
Article1 在开发中可能会有这样的情景.需要在容器启动的时候执行一些内容.比如读取配置文件,数据库连接之类的.SpringBoot给我们提供了两个接口来帮助我们实现这种需求.这两个接口分别为Co ...
随机推荐
- 深入理解snp-calling流程——转载
------------恢复内容开始------------ GATK4流程 准备配套数据 明确参考基因组版本!!!b36/b37/hg18/hg19/hg38,记住b37和hg19并不是完全一样的, ...
- nuxt中asyncData和fetch的区别
asyncData作用于页面pages,在组件中不能使用,并且asyncData中没有this,如果想要给data中的数据赋值,要在asyncData函数中return出去 fetch 作用于组件中c ...
- Bugku Log4j2 漏洞题目 解题参考
Log4j2 漏洞题目 题目地址 https://ctf.bugku.com/challenges/detail/id/340.html?page=1 二.攻击环境准备 需要一台linux云服务器,把 ...
- Latex Algorithm 语法错误导致无法编译
遇到了几种情况: 1. for 循环没加{} 2. $\textbf{T_i}$, 想要加粗T,但是把i也扩进去了,latex就不懂了,于是一直recompile不出来说超时什么什么的,把i放到外面就 ...
- LeetCode刷题感想之滑动窗口
发现滑动窗口也是一种经典解题思路,这一篇简单聊一下滑动窗口. 通常在碰到求XX子数组,子字符串,连续XX等题眼,可以考试用滑动窗口的思路来解决问题. 窗口的类型有几种: 1. 固定长度的窗口. 2. ...
- k8s集群部署kafka
一.部署步骤 1.部署NFS并挂载共享目录 2.部署zookeeper集群 3.部署kafka集群 4.测试kafka 二.部署NFS并挂载共享目录 注:使用云产品的NAS存储可跳过此步骤 1.服务端 ...
- MySQL核心知识
MySQL常用的命令 启动:net start mySql; 进入:mysql -u root -p/mysql -h localhost -u root -p databaseName; 列出数据库 ...
- Mysql压缩版安装
1.官网去下载压缩版本(https://dev.mysql.com/downloads/) 这里下载Community版(因为免费<-_-> 它功能和Enterprise版功能差不多) ...
- ES搜索高亮分词排序聚合搜索
分词高亮搜索代码 List<A> list = new ArrayList<>(); //设置高亮显示 HighlightBuilder highlightBuilder = ...
- VisualVM无法运行,修改配置文件
在VisualVM安装位置下找到etc目录修改etc目录下的visualvm.conf文件 加入配置 参数 指定JDK或JRE路径,如 visualvm_jdkhome="C:\xxx\ ...