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启动器的更多相关文章

  1. springboot(十五):springboot+jpa+thymeleaf增删改查示例

    这篇文章介绍如何使用jpa和thymeleaf做一个增删改查的示例. 先和大家聊聊我为什么喜欢写这种脚手架的项目,在我学习一门新技术的时候,总是想快速的搭建起一个demo来试试它的效果,越简单越容易上 ...

  2. SpringBoot(十五)_springboot实现预览pdf

    最近,项目上要做个打印的东西,还要预览.我想就直接生成pdf预览,然后用户选择打印 于是,昨天找了找资料.一般用itext 进行转pdf.于是我就用springboot试了试,代码比较简单,现在只是简 ...

  3. SpringBoot(十五):SpringBoot2.x集成eureka实现注高可用册中心,高可用的服务器提供者,以及消费者示例

    本文代码请参考<https://github.com/478632418/springcloud-eureka-server-client/tree/master/mall>.<ht ...

  4. springboot基础项目搭建(十五篇)

    springboot系列一.springboot产生背景及介绍 springboot系列二.springboot项目搭建 springboot系列三.springboot 单元测试.配置访问路径.多个 ...

  5. Gradle 1.12用户指南翻译——第三十五章. Sonar 插件

    本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...

  6. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十五)阶段总结

    作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 一 每个阶段在结尾时都会有一个阶段总结,在<SSM整合基础篇& ...

  7. springboot(十四):springboot整合shiro-登录认证和权限管理(转)

    springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...

  8. Java基础之十五 泛型

    第十五章 泛型 一般的类和方法,只能使用具体的类型:要么是基本类型,要么是自定义类型.如果要编写可以应用于多种类型的代码,这种刻板的限制对代码的束缚就会很大. 在面对对象编程语言中,多态算是一种泛化机 ...

  9. 跟我学SpringCloud | 第十五篇:微服务利剑之APM平台(一)Skywalking

    目录 SpringCloud系列教程 | 第十五篇:微服务利剑之APM平台(一)Skywalking 1. Skywalking概述 2. Skywalking主要功能 3. Skywalking主要 ...

随机推荐

  1. HDU 5818 Joint Stacks (优先队列)

    Joint Stacks 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5818 Description A stack is a data stru ...

  2. MacPorts镜像

    /opt/local/etc/macports/macports.conf: rsync_server pek.cn.rsync.macports.org rsync_dir macports/rel ...

  3. LeetCode 31. 下一个排列(Next Permutation)

    题目描述 实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列. 如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列). 必须原地修改,只允许使用额外常 ...

  4. LeetCode 55. 跳跃游戏(Jump Game)

    题目描述 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] 输出: ...

  5. Gson解析list类型的json串

    Gson gson = new Gson(); Type type = new TypeToken<List<Object>>() {}.getType(); List< ...

  6. assert断言用法

    使用assert断言是学习python一个非常好的习惯,python assert 断言句语格式及用法很简单.在没完善一个程序之前,我们不知道程序在哪里会出错,与其让它在运行最崩溃,不如在出现错误条件 ...

  7. 谈谈 Android 的优点和不足之处?

    优点:1.开放性,开源,免费,可定制2.挣脱运营商束缚3.丰富的硬件选择4.不受任何限制的开发商5.无缝结合的 Google 应用缺点:1.安全问题.隐私问题2.同质化严重3.运营商对 Android ...

  8. if-else判断语句

    <1>if-else的使用格式 if 条件: 满足条件时要做的事情1 满足条件时要做的事情2 满足条件时要做的事情3 ...(省略)... else: 不满足条件时要做的事情1 不满足条件 ...

  9. [Python]操作shell脚本报错Permission denied

    问题: In []: os.system('./test_shell_no_para.sh') : ./test_shell_no_para.sh: Permission denied Out[]: ...

  10. ControlTemplate in WPF —— TreeView

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...