服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 详解Spring Boot 项目启动时执行特定方法

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

2021-05-09 12:44月未明 Java教程

这篇文章主要介绍了详解Spring Boot 项目启动时执行特定方法,Springboot给我们提供了两种“开机启动”某些方法的方式:ApplicationRunner和CommandLineRunner。感兴趣的小伙伴们可以参考一下

springboot给我们提供了两种“开机启动”某些方法的方式:applicationrunner和commandlinerunner。

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

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

先看看commandlinerunner :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 :

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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接口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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注解实现方式:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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!");
  }
 
}

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

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

原文链接:https://blog.csdn.net/qq_35981283/article/details/77826537

延伸 · 阅读

精彩推荐