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主要 ...
随机推荐
- [CSP-S模拟测试]:线性代数(模拟)
题目传送门(内部题113) 输入格式 第一行一个正整数$n$. 接下来$n$行,每行$n$个整数,描述$C$矩阵.保证输入的是一个林先森矩阵. 输出格式 若不可能实现,则输出一行$Impossible ...
- 分布式-信息方式-ActiveMQ构建应用
ActivemQ构建应用Broker:相当于一个 ActiveMQ服务器实例命令行启动参数示例如下:1 ...
- 【Spark机器学习速成宝典】基础篇01Windows下spark开发环境搭建+sbt+idea(Scala版)
注意: spark用2.1.1 scala用2.11.11 材料准备 spark安装包 JDK 8 IDEA开发工具 scala 2.11.8 (注:spark2.1.0环境于scala2.11环境开 ...
- 一、基础篇--1.2Java集合-ArrayList和Vector的区别
ArrayList和Vector的区别 ArrayList和Vector都是基于动态数组实现的. 区别 ArrayList是非线程安全的,Vector是线程安全的. Vector的方法都加了同步锁 ...
- Android中@id与@+id区别和sharedUserId属性详解
Android中的组件需要用一个int类型的值来表示,这个值也就是组件标签中的id属性值. id属性只能接受资源类型的值,也就是必须以@开头的值,例如,@id/abc.@+id/xyz等. 如果在@后 ...
- [Flask]使用sqlite数据库
app.py from flask import Flask from flask_sqlalchemy import SQLAlchemy import os basedir = os.path.a ...
- [flask]分页显示列表
添加分页支持的视图函数 app.py @app.route('/search') def search(): page = request.args.get('page', 1, type=int) ...
- redhat下配置SEED DVS6446开发环境3
1.运行arm_v5t_le-gcc,报错为: 64bit的Linux操作系统,无法运行32bit的应用程序 /lib/ld-linux.so.2: bad ELF interpreter: No s ...
- 【深度聚类】Superpixel Sampling Networks
Superpixel Sampling Networks 原始文档:https://www.yuque.com/lart/papers/ssn 本文的思想很简单,传统的超像素算法是一种有效的低/中级的 ...
- python基础--面向对象之绑定非绑定方法
# 类中定义的函数分为两大类, #一,绑定方法(绑定给谁,谁来调用就自动将它本身当做第一个参数传入) # 1,绑定到类的方法:用classmethod装饰器装饰的方法. # 对象也可以掉用,仍将类作为 ...