详解Spring Boot 项目启动时执行特定方法

 更新时间:2018年06月13日 14:51:54   作者:月未明  
这篇文章主要介绍了详解Spring Boot 项目启动时执行特定方法,Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner。感兴趣的小伙伴们可以参考一下

Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner。

这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方法。我们可以通过实现ApplicationRunner和CommandLineRunner,来实现,他们都是在SpringApplication 执行之后开始执行的。

CommandLineRunner接口可以用来接收字符串数组的命令行参数,ApplicationRunner 是使用ApplicationArguments 用来接收参数的,貌似后者更牛逼一些。

先看看CommandLineRunner :

package com.springboot.study;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
 * Created by pangkunkun on 2017/9/3.
 */
@Component
public class MyCommandLineRunner implements CommandLineRunner{

  @Override
  public void run(String... var1) throws Exception{
    System.out.println("This will be execute when the project was started!");
  }
}

ApplicationRunner :

package com.springboot.study;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * Created by pangkunkun on 2017/9/3.
 */
@Component
public class MyApplicationRunner implements ApplicationRunner {

  @Override
  public void run(ApplicationArguments var1) throws Exception{
    System.out.println("MyApplicationRunner class will be execute when the project was started!");
  }

}

这两种方式的实现都很简单,直接实现了相应的接口就可以了。记得在类上加@Component注解。

如果想要指定启动方法执行的顺序,可以通过实现org.springframework.core.Ordered接口或者使用org.springframework.core.annotation.Order注解来实现。

这里我们以ApplicationRunner 为例来分别实现。

Ordered接口:

package com.springboot.study;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * Created by pangkunkun on 2017/9/3.
 */
@Component
public class MyApplicationRunner implements ApplicationRunner,Ordered{


  @Override
  public int getOrder(){
    return 1;//通过设置这里的数字来知道指定顺序
  }

  @Override
  public void run(ApplicationArguments var1) throws Exception{
    System.out.println("MyApplicationRunner1!");
  }

}

Order注解实现方式:

package com.springboot.study;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Created by pangkunkun on 2017/9/3.
 * 这里通过设定value的值来指定执行顺序
 */
@Component
@Order(value = 1)
public class MyApplicationRunner implements ApplicationRunner{

  @Override
  public void run(ApplicationArguments var1) throws Exception{
    System.out.println("MyApplicationRunner1!");
  }

}

这里不列出其他对比方法了,自己执行下就好。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 基于Java中字符串内存位置详解

    基于Java中字符串内存位置详解

    下面小编就为大家带来一篇基于Java中字符串内存位置详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-08-08
  • Java如何通过jstack命令查询日志

    Java如何通过jstack命令查询日志

    在分析线上问题时常使用到jstack <PID>命令将当时Java应用程序的线程堆栈dump出来,面对jstack 日志,我们如何查看?下面小编给大家介绍下Java如何通过jstack命令查询日志,感兴趣的朋友一起看看吧
    2023-03-03
  • Spring框架十一种常见异常的解决方法汇总

    Spring框架十一种常见异常的解决方法汇总

    今天小编就为大家分享一篇关于Spring框架十一种常见异常的解决方法汇总,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-03-03
  • Java 中的 @SneakyThrows 注解的使用方法(简化异常处理的利与弊)

    Java 中的 @SneakyThrows 注解的使用方法(简化异常处理的利与弊)

    @SneakyThrows是Lombok提供的一个注解,用于简化Java方法中的异常处理,特别是对于检查型异常,它允许方法抛出异常而不必显式声明或捕获这些异常,本文介绍Java 中的 @SneakyThrows 注解的使用方法,感兴趣的朋友一起看看吧
    2025-03-03
  • java获取系统路径字体、得到某个目录下的所有文件名、获取当前路径

    java获取系统路径字体、得到某个目录下的所有文件名、获取当前路径

    这篇文章主要介绍了java获取系统路径字体、得到某个目录下的所有文件名、获取当前路径,需要的朋友可以参考下
    2014-04-04
  • Spring Security登录接口兼容JSON格式登录实现示例

    Spring Security登录接口兼容JSON格式登录实现示例

    前后端分离中,前端和后端的数据交互通常是JSON格式,本文主要介绍了Spring Security登录接口兼容JSON格式登录实现示例,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • 一文详解Java如何创建和销毁对象

    一文详解Java如何创建和销毁对象

    Java由Sun Microsystems发明并在1995年发布,是世界上使用最广泛的编程语言之一。本文主要和大家介绍一下Java是如何创建和销毁对象的,希望对大家有所帮助
    2022-11-11
  • java连接SQL Server数据库的超详细教程

    java连接SQL Server数据库的超详细教程

    最近在java连接SQL数据库时会出现一些问题,所以这篇文章主要给大家介绍了关于java连接SQL Server数据库的超详细教程,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • Springboot框架实现自动装配详解

    Springboot框架实现自动装配详解

    在使用springboot时,很多配置我们都没有做,都是springboot在帮我们完成,这很大一部分归功于springboot自动装配。本文将详细为大家讲解SpringBoot的自动装配原理,需要的可以参考一下
    2022-08-08
  • 通过AOP环绕通知如何实现事务控制

    通过AOP环绕通知如何实现事务控制

    这篇文章主要介绍了通过AOP环绕通知如何实现事务控制的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09

最新评论