springboot(十五)-Runner启动器
Runner启动器
如果你想在Spring Boot启动的时候运行一些特定的代码,你可以实现接口ApplicationRunner
或者CommandLineRunner
,这两个接口实现方式一样,它们都只提供了一个run方法。
CommandLineRunner:启动获取命令行参数。
public interface CommandLineRunner { /**
* Callback used to run the bean.
* @param args incoming main method arguments
* @throws Exception on error
*/
void run(String... args) throws Exception; }
ApplicationRunner:启动获取应用启动的时候参数。
public interface ApplicationRunner { /**
* Callback used to run the bean.
* @param args incoming application arguments
* @throws Exception on error
*/
void run(ApplicationArguments args) throws Exception; }
使用方式
import org.springframework.boot.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.*; @Component
public class MyBean implements CommandLineRunner { public void run(String... args) {
System.out.println("haha");
} @Bean
public CommandLineRunner init() { return (String... strings) -> {
System.out.println("enen");
}; } }
提供两个方法,一个实现了接口提供的方法,一个自己定义一个Bean。我们看下效果。
05-09 18:10:07.920 INFO [com.cetc.cks.Application] - Starting Application on LAPTOP-3HL6RUMK with PID 3012 (D:\workspaces\pro_cks_manager\target\classes started by 18811 in D:\workspaces\pro_cks_manager)
05-09 18:10:07.920 INFO [com.cetc.cks.Application] - No active profile set, falling back to default profiles: default
05-09 18:10:08.436 INFO [org.apache.catalina.core.StandardService] - Starting service [Tomcat]
05-09 18:10:08.436 INFO [org.apache.catalina.core.StandardEngine] - Starting Servlet Engine: Apache Tomcat/8.5.23
05-09 18:10:08.498 INFO [org.apache.jasper.servlet.TldScanner] - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
05-09 18:10:08.498 INFO [o.a.c.core.ContainerBase.[Tomcat].[localhost].[/]] - Initializing Spring embedded WebApplicationContext
05-09 18:10:08.764 INFO [org.hibernate.jpa.internal.util.LogHelper] - HHH000204: Processing PersistenceUnitInfo [
name: default
...]
05-09 18:10:08.795 INFO [org.hibernate.dialect.Dialect] - HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
haha
enen
05-09 18:10:09.025 INFO [com.cetc.cks.Application] - Started Application in 1.148 seconds (JVM running for 414.131)
由控制台的log可知,都执行了。
我们实现另一个:
@Component
public class MyBean implements ApplicationRunner { // public void run(String... args) {
// System.out.println("haha");
// } @Bean
public CommandLineRunner init() { return (String... strings) -> {
System.out.println("enen");
}; } @Override
public void run(ApplicationArguments args) throws Exception {
// TODO Auto-generated method stub
System.out.println("666");
} }
同样,我们提供两个方法,一个实现接口的方法,还有一个自定义一个Bean。
05-09 18:12:58.234 INFO [com.cetc.cks.Application] - Starting Application on LAPTOP-3HL6RUMK with PID 3012 (D:\workspaces\pro_cks_manager\target\classes started by 18811 in D:\workspaces\pro_cks_manager)
05-09 18:12:58.234 INFO [com.cetc.cks.Application] - No active profile set, falling back to default profiles: default
05-09 18:12:58.703 INFO [org.apache.catalina.core.StandardService] - Starting service [Tomcat]
05-09 18:12:58.703 INFO [org.apache.catalina.core.StandardEngine] - Starting Servlet Engine: Apache Tomcat/8.5.23
05-09 18:12:58.766 INFO [org.apache.jasper.servlet.TldScanner] - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
05-09 18:12:58.781 INFO [o.a.c.core.ContainerBase.[Tomcat].[localhost].[/]] - Initializing Spring embedded WebApplicationContext
05-09 18:12:59.109 INFO [org.hibernate.jpa.internal.util.LogHelper] - HHH000204: Processing PersistenceUnitInfo [
name: default
...]
05-09 18:12:59.125 INFO [org.hibernate.dialect.Dialect] - HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
666
enen
05-09 18:12:59.390 INFO [com.cetc.cks.Application] - Started Application in 1.212 seconds (JVM running for 584.496)
由控制台的log可知,都执行了。而且都是先执行重写接口的方法。
springboot(十五)-Runner启动器的更多相关文章
- springboot(十五):springboot+jpa+thymeleaf增删改查示例
这篇文章介绍如何使用jpa和thymeleaf做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个demo来试试它的效果,越简单越容易上 ...
- SpringBoot(十五)_springboot实现预览pdf
最近,项目上要做个打印的东西,还要预览.我想就直接生成pdf预览,然后用户选择打印 于是,昨天找了找资料.一般用itext 进行转pdf.于是我就用springboot试了试,代码比较简单,现在只是简 ...
- SpringBoot(十五):SpringBoot2.x集成eureka实现注高可用册中心,高可用的服务器提供者,以及消费者示例
本文代码请参考<https://github.com/478632418/springcloud-eureka-server-client/tree/master/mall>.<ht ...
- springboot基础项目搭建(十五篇)
springboot系列一.springboot产生背景及介绍 springboot系列二.springboot项目搭建 springboot系列三.springboot 单元测试.配置访问路径.多个 ...
- Gradle 1.12用户指南翻译——第三十五章. Sonar 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十五)阶段总结
作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 一 每个阶段在结尾时都会有一个阶段总结,在<SSM整合基础篇& ...
- springboot(十四):springboot整合shiro-登录认证和权限管理(转)
springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...
- Java基础之十五 泛型
第十五章 泛型 一般的类和方法,只能使用具体的类型:要么是基本类型,要么是自定义类型.如果要编写可以应用于多种类型的代码,这种刻板的限制对代码的束缚就会很大. 在面对对象编程语言中,多态算是一种泛化机 ...
- 跟我学SpringCloud | 第十五篇:微服务利剑之APM平台(一)Skywalking
目录 SpringCloud系列教程 | 第十五篇:微服务利剑之APM平台(一)Skywalking 1. Skywalking概述 2. Skywalking主要功能 3. Skywalking主要 ...
随机推荐
- Yarn 内存分配管理机制及相关参数配置
上一篇hive on tez 任务报错中提到了containter内存不足,现对yarn 内存分配管理进行介绍 一.相关配置情况 关于Yarn内存分配与管理,主要涉及到了ResourceManage. ...
- xgboost原理与实战
目录 xgboost原理 xgboost和gbdt的区别 xgboost安装 实战 xgboost原理 xgboost是一个提升模型,即训练多个分类器,然后将这些分类器串联起来,达到最终的预测效果.每 ...
- (组件的)状态(state)和属性(props)之间有何不同
State 是一种数据结构,用于组件挂载时所需数据的默认值.State 可能会随着时间的推移而发生突变,但多数时候是作为用户事件行为的结果.Props(properties 的简写)则是组件的配置.p ...
- D4下午
开始学图论辣 图的基本模型 图是点和边组成的集合体,G = <V, E> v是点集,e是边集 还有就是有向图无向图啥的 算了太水了不写了 提几个没大见过的吧 环 环上任意两点间可以随意到达 ...
- Oracle中如何生成随机数字、随机字符串、随机日期
.随机小数 dbms_random.value(low,high): --获取一个[low,high)之间的小数,包含low,不包含high 可以结合trunc函数获取整数 例如: select db ...
- cocoapods [!] Unable to find a pod with name, author, summary, or description matching `xx`
pod search MJRefresh的时候报错 [!] Unable to find a pod with name, author, summary, or description matchi ...
- jQuery.validator.addMethod自定义验证
jQuery.validator.addMethod("numOrLetter", function(value, element) { return this.optional( ...
- OpenStack 启动虚拟机 Booting from Hard Disk
问题 OpenStack 启动虚拟机 Booting from Hard Disk-GRUB 环境 OpenStack RUNNING IN vSphere 6.0.0 VM 开启了 CPU 虚拟化支 ...
- 三十七:数据库之SQLAlchemy外建之多对多关系
准备工作 from sqlalchemy import create_engine, Column, Integer, String, Float, Text, ForeignKeyfrom sqla ...
- Linux 查找当前目录下 包含特定字符串 的所有文件
使用 Linux 经常会遇到这种情况:只知道文件中包含某些特定的字符串,但是不知道具体的文件名.需要根据“特定的字符串”反向查找文件. 示例(路径文件如下): ./miracle/luna/a.txt ...