SpringBoot_启动后执行
2025-01-22 08:19:30    874 字   
This post is also available in English and alternative languages.

Spring Framework版本:5.1.6.RELEASE (点击跳转官方文档)
SpringBoot版本:2.1.4.RELEASE (点击跳转官方文档)

SpringBoot 启动后运行。


1. PostConstruct 注解

使用 @PostConstruct 注解,但要注意其中执行的方法不能耗时过长,否则会导致方法执行期间无法提供服务。

1
2
3
4
5
6
7
8
@Slf4j
@Component
public class CustomPostConstruct {
@PostConstruct
public void initDoSomething() {
log.info("PostConstruct#initDoSomething === 初始化做点什么呢?");
}
}

2. ApplicationRunner 接口

ApplicationRunner 接口实现的 run 方法接收的参数,同 main(String[] args) 主函数接收的参数一致,意味着可以对命令行启动时传入的参数进行一些处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Slf4j
@Component
@Order(value = 1)
public class CustomApplicationRunner01 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("CustomApplicationRunner01#run === 初始化运行点什么呢?");
}
}

@Slf4j
@Component
@Order(value = 2)
public class CustomApplicationRunner02 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
log.info("CustomApplicationRunner02#run === 初始化运行点什么呢?");
}
}

多个 ApplicationRunner 接口可以通过 @Order 注解,配置其加载顺序,数值越小优先级越高,运行结果如下:

1
2
c.y.r.m.CustomApplicationRunner02        : CustomApplicationRunner01#run === 初始化运行点什么呢?
c.y.r.m.CustomApplicationRunner01 : CustomApplicationRunner02#run === 初始化运行点什么呢?

将 CustomApplicationRunner01 类上 @Order 值调整为 5,再看下运行结果:

1
2
CustomApplicationRunner02#run === 初始化运行点什么呢?
CustomApplicationRunner01#run === 初始化运行点什么呢?

3. CommandLineRunner 接口

CommandLineRunner 接口实现的 run 方法接收的参数,同 main(String[] args) 主函数接收的参数一致,意味着可以对命令行启动时传入的参数进行一些处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Slf4j
@Component
@Order(value = 6)
public class CustomCommandLineRunner01 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("CustomCommandLineRunner01#run === 初始化运行点什么呢?");
}
}

@Slf4j
@Component
@Order(value = 3)
public class CustomCommandLineRunner02 implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
log.info("CustomCommandLineRunner02#run === 初始化运行点什么呢?");
}
}

多个 CommandLineRunner 接口可以通过 @Order 注解,配置其加载顺序,数值越小优先级越高,运行结果如下:

1
2
CustomCommandLineRunner02#run === 初始化运行点什么呢?
CustomCommandLineRunner01#run === 初始化运行点什么呢?

4. 依赖POM

以上示例的依赖

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<dependencies>
<!--starter-->
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>

<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
</dependencies>