Spring Boot ApplicationReadyEvent
最后修改于 2023 年 7 月 29 日
在本文中,我们将展示如何在应用程序准备就绪时执行任务。
Spring Boot 应用程序会发出各种事件。我们可以使用监听器来响应这些事件。
例如,ApplicationStartedEvent 在上下文刷新后、任何应用程序和命令行运行器被调用之前发送。ApplicationReadyEvent 在任何应用程序和命令行运行器被调用后发送。它表明应用程序已准备好服务请求。
Spring Boot ApplicationReadyEvent 示例
以下 Spring Boot 应用程序是一个简单的 Web 应用程序,它响应 ApplicationStartedEvent 触发 Web 请求。该请求是使用 WebClient 发出的。
build.gradle
...
src
├───main
│ ├───java
│ │ └───com
│ │ └───zetcode
│ │ │ Application.java
│ │ ├───bean
│ │ │ TimeResponse.java
│ │ ├───event
│ │ │ AppEvents.java
│ │ └───route
│ │ AppRoutes.java
│ └───resources
└───test
└───java
└───com
└───zetcode
└───route
AppRoutesTest.java
这是项目结构。
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
}
group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webflux'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
这是 Gradle 构建文件。spring-boot-starter-webflux 是用于使用 Spring Framework 的 Reactive Web 支持构建 WebFlux 应用程序的启动器。
package com.zetcode.bean;
public class TimeResponse {
private String date;
private Long unixtime;
private String time;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public Long getUnixtime() {
return unixtime;
}
public void setUnixtime(Long unixtime) {
this.unixtime = unixtime;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("TimeResponse{");
sb.append("date='").append(date).append('\'');
sb.append(", unixtime=").append(unixtime);
sb.append(", time='").append(time).append('\'');
sb.append('}');
return sb.toString();
}
}
这是 TimeResponse bean。它用于存储来自 Web 请求的数据。
package com.zetcode.event;
import com.zetcode.bean.TimeResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Component
public class AppEvents {
private static final Logger logger = LoggerFactory.getLogger(AppEvents.class);
@EventListener(ApplicationReadyEvent.class)
public void startApp() {
var webClient = WebClient.create("http://time.jsontest.com/");
Mono<TimeResponse> result = webClient.get()
.retrieve()
.bodyToMono(TimeResponse.class);
result.subscribe(res -> logger.info("{}", res));
}
}
在 AppEvents 中,我们创建一个简单的 GET 请求。
@EventListener(ApplicationReadyEvent.class)
public void startApp() {
使用 @EventListener 注解,我们注册了 ApplicationReadyEvent。
var webClient = WebClient.create("http://time.jsontest.com/");
从 http://time.jsontest.com/,我们获取当前时间。
Mono<TimeResponse> result = webClient.get()
.retrieve()
.bodyToMono(TimeResponse.class);
result.subscribe(res -> logger.info("{}", res));
使用 WebClient,我们创建一个 GET 请求到该站点并将结果输出到终端。
package com.zetcode.route;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
@Configuration
public class AppRoutes {
@Bean
RouterFunction<ServerResponse> home() {
return route(GET("/"), request -> ok().bodyValue("Home page"));
}
}
我们的应用程序使用一个函数式 Web 框架来为首页返回一条简单的消息。
package com.zetcode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Application 设置 Spring Boot 应用程序
package com.zetcode.route;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.reactive.server.WebTestClient;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AppRoutesTest {
@Autowired
private WebTestClient client;
@Test
public void test_home_page() {
client.get().uri("/").exchange().expectStatus().isOk()
.expectBody(String.class).isEqualTo("Home page");
}
}
使用 WebTestClient,我们为首页创建一个测试方法。
...
... com.zetcode.event.AppEvents: TimeResponse{date='07-18-2023', unixtime=null, time='11:02:38 AM'}
...
当应用程序启动时,我们会在终端上收到此消息。
在本文中,我们使用了 ApplicationReadyEvent。