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主要 ...
随机推荐
- Boost学习
使用boost function使用类成员函数作为回调函数 #include <iostream> #include <boost/function.hpp> struct M ...
- 剑指offer32----之字形打印一颗二叉树
题目 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. 思路 在这里我们如果单纯的使用队列去弄的话,会很 ...
- vue项目内嵌入到app input type=file 坑(文件上传插件)
w问题描述: 我用vue-cli完成的一个移动端项目,内嵌到app当中,用原生的input type=file 来完成文件上传.在安卓下没有问题但是在苹果手机 上传第二次手机就会发生白屏 并无缘无故跳 ...
- 函数-this
1.this. 解析器在调用函数的时候,每次都会向函数内部传递进一个隐含的参数(即this): this指向一个对象,这个对象称为函数执行的上下文对象.根据函数的调用方式的不同,this会指向不同的对 ...
- 快读模板&&快出模板
inline int read() { ,b=; char c=getchar(); ') { if(c=='-') b=-; c=getchar(); } ') { a=(a<<)+(a ...
- MyEclipse下复制的web项目
MyEclipse下复制的web项目,需要修改MyEclipse->Project Facets->Web
- Linux 下 *.tar.gz 文件解压缩命令及错误处理
1.压缩命令: 命令格式: tar -zcvf 压缩文件名 .tar.gz 被压缩文件名 可先切换到当前目录下,压缩文件名和被压缩文件名都可加入路径. 2.解压缩命令: 命令格式: tar -zxvf ...
- 简单的python下载器
最近在玩爬虫,有时候会爬下来很多感兴趣文件的连接. 如果自己手动下载它们的话工作量实在太大. 于是,简单写了个下载小脚本: import os, urllib2 os.chdir(r'd:') url ...
- mybatis的xml文件中的CDATA的使用
mybatis的xml文件中的CDATA的使用 <!--查询列表--> <select id="queryListPage" parameterType=&quo ...
- zk 文件存储
zk 有 2 种文件,快照和事务日志,快照是某一时刻的全量数据,事务日志中记录了数据的修改事件. 快照的文件名是 snapshot.zxid,zxid 是当前最大的事务 id // org.apach ...