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主要 ...
随机推荐
- eclipse中取消自动生成的TODO Auto-generated method stub
我们在实现接口定义的方法.Eclipse往往会自动加上一句:TODO Auto-generated method stub 每次手动删除很麻烦,我们可以设置一下,让强大的Eclipse在完成自动代码时 ...
- C++入门经典-例2.8-输出整数,控制打印格式
1:代码如下: // 2.8.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #incl ...
- Netflix的Ribbon主要负载均衡策略
1.简单轮询负载均衡 2.加权响应时间负载均衡 3.随机负载均衡 4.区域感知轮询负载均衡
- spring的AOP基本原理
一.什么是AOP AOP(Aspect Oriented Programming),意思是面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP基于IoC基础,是对OOP ...
- 使用FFmpeg让mp4转gif
配好环境之后,需要在开始菜单中打开命令提示符,然后进入到test.mp4的文件目录下执行命令.(直接在文件目录下打开cmd不能生效)ffmpeg -i test.mp4 -f gif test.gif
- charts_03
table 数值获取: 1.http://www.w3school.com.cn/jsref/dom_obj_all.asp 2.http://blog.csdn.net/xs_zgsc/articl ...
- kolla-ansible-----常用命令
常用命令 kolla-ansible prechecks -i multinode #部署前环境检测 kolla-genpwd #生成/etc/kolla/password.yml密码配置文件 kol ...
- Gradle 依赖
在开发中,我们经常使用compile,api,implementation引入库,这三种是有区别的. 1 api和compile api和compile关键字作用效果是一样的,使用时可以互相替换. 实 ...
- web开发(九) 使用javamail进行发送邮件,(使用QQ,163,新浪邮箱服务器)
在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6506027.html>,在此仅供学习参考之用. 一.发送 ...
- 【计算机视觉】阶编码本模型(Multi phase codebook model)
转自:http://www.cnblogs.com/xrwang/archive/2012/04/24/MPCBBGM.html 多阶编码本模型(Multi phase codebook model) ...