<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Spring Boot `ApplicationReadyEvent` 教程 > 原文: [http://zetcode.com/springboot/applicationreadyevent/](http://zetcode.com/springboot/applicationreadyevent/) Spring Boot `ApplicationReadyEvent`教程展示了在應用就緒后如何執行任務。 Spring Boot 應用發出各種事件。 我們可以使用監聽器對此類事件做出反應。 例如,`ApplicationStartedEvent`是在刷新上下文之后但在調用任何應用和命令行運行程序之前發送的。 在調用任何應用和命令行運行程序之后,將發送`ApplicationReadyEvent`。 它指示該應用已準備就緒,可以處理請求。 ## Spring Boot `ApplicationReadyEvent`示例 以下 Spring Boot 應用是一個簡單的 Web 應用,可響應`ApplicationStartedEvent`觸發 Web 請求。 該請求是通過`WebClient`發出的。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───bean │ │ │ TimeResponse.java │ │ ├───event │ │ │ AppEvents.java │ │ └───routes │ │ AppRoutes.java │ └───resources └───test └───java └───com └───zetcode └───routes AppRoutesTest.java ``` 這是項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>applicationreadyevent</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 這是 Maven 構建文件。 `spring-boot-starter-webflux`是使用 Spring Framework 的反應式 Web 支持構建 WebFlux 應用的入門。 `com/zetcode/bean/TimeResponse.java` ```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` ```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 請求。 ```java @EventListener(ApplicationReadyEvent.class) public void startApp() { ``` 使用`@EventListener`注解,我們注冊`ApplicationReadyEvent`。 ```java var webClient = WebClient.create("http://time.jsontest.com/"); ``` 從`http://time.jsontest.com/`中,我們可以獲得當前時間。 ```java Mono<TimeResponse> result = webClient.get() .retrieve() .bodyToMono(TimeResponse.class); result.subscribe(res -> logger.info("{}", res)); ``` 使用`WebClient`,我們向站點創建一個 GET 請求,并將結果輸出到終端。 `com/zetcode/routes/AppRoutes.java` ```java package com.zetcode.routes; 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().syncBody("Home page")); } } ``` 我們的應用使用函數式的 Web 框架來返回首頁的簡單消息。 `com/zetcode/Application.java` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`設置 Spring Boot 應用 `com/zetcode/routes/AppRoutesTest.java` ```java package com.zetcode.routes; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.reactive.server.WebTestClient; @RunWith(SpringRunner.class) @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`,我們為主頁創建一種測試方法。 ```java ... 2019-06-21 16:40:45.214 INFO 9356 --- [ctor-http-nio-5] com.zetcode.event.AppEvents : TimeResponse{date='06-21-2019', unixtime=null, time='02:40:47 PM'} ... ``` 當應用啟動時,我們將此消息發送到終端。 列出[所有 Spring Boot 教程](/all/#springboot)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看