ZetCode

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

这是项目结构。

build.gradle
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 应用程序的启动器。

com/zetcode/bean/TimeResponse.java
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 请求的数据。

com/zetcode/event/AppEvents.java
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 请求到该站点并将结果输出到终端。

com/zetcode/route/AppRoutes.java
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 框架来为首页返回一条简单的消息。

com/zetcode/Application.java
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 应用程序

com/zetcode/route/AppRoutesTest.java
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

作者

我叫 Jan Bodnar,是一位充满激情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。迄今为止,我撰写了 1,400 多篇文章和 8 本电子书。我拥有超过十年的编程教学经验。

列出 所有 Spring Boot 教程