|
16 | 16 |
|
17 | 17 | package org.springframework.web.server.adapter;
|
18 | 18 |
|
19 |
| -import java.nio.charset.StandardCharsets; |
20 |
| -import java.util.Collections; |
21 |
| -import java.util.concurrent.atomic.AtomicBoolean; |
22 |
| -import java.util.function.BiFunction; |
23 |
| - |
24 | 19 | import org.junit.jupiter.api.Test;
|
25 |
| -import reactor.core.publisher.Flux; |
26 |
| -import reactor.core.publisher.Mono; |
27 |
| - |
28 | 20 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
29 | 21 | import org.springframework.context.annotation.Bean;
|
30 | 22 | import org.springframework.context.annotation.Configuration;
|
|
33 | 25 | import org.springframework.core.io.buffer.DefaultDataBufferFactory;
|
34 | 26 | import org.springframework.http.HttpHeaders;
|
35 | 27 | import org.springframework.http.server.reactive.HttpHandler;
|
| 28 | +import org.springframework.http.server.reactive.HttpHandlerDecoratorFactory; |
36 | 29 | import org.springframework.http.server.reactive.ServerHttpRequest;
|
37 | 30 | import org.springframework.web.server.ServerWebExchange;
|
38 | 31 | import org.springframework.web.server.WebExceptionHandler;
|
39 | 32 | import org.springframework.web.server.WebFilter;
|
40 | 33 | import org.springframework.web.server.WebHandler;
|
41 | 34 | import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest;
|
42 | 35 | import org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse;
|
| 36 | +import reactor.core.publisher.Flux; |
| 37 | +import reactor.core.publisher.Mono; |
| 38 | + |
| 39 | +import java.nio.charset.StandardCharsets; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 42 | +import java.util.concurrent.atomic.AtomicLong; |
| 43 | +import java.util.function.BiFunction; |
43 | 44 |
|
44 | 45 | import static java.time.Duration.ofMillis;
|
45 | 46 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -139,12 +140,82 @@ void httpHandlerDecorator() {
|
139 | 140 | assertThat(success.get()).isTrue();
|
140 | 141 | }
|
141 | 142 |
|
| 143 | + @Test |
| 144 | + void httpHandlerDecoratorBeans() { |
| 145 | + |
| 146 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); |
| 147 | + context.register(HttpHandlerDecoratorBeansConfig.class); |
| 148 | + context.refresh(); |
| 149 | + HttpHandler builder = WebHttpHandlerBuilder.applicationContext(context).build(); |
| 150 | + |
| 151 | + builder.handle(MockServerHttpRequest.get("/").build(), new MockServerHttpResponse()).block(); |
| 152 | + |
| 153 | + AtomicLong decorator1NanoTime = context.getBean("decorator1NanoTime", AtomicLong.class); |
| 154 | + AtomicLong decorator2NanoTime = context.getBean("decorator2NanoTime", AtomicLong.class); |
| 155 | + AtomicLong decorator3NanoTime = context.getBean("decorator3NanoTime", AtomicLong.class); |
| 156 | + assertThat(decorator1NanoTime).hasValueLessThan(decorator3NanoTime.get()); |
| 157 | + assertThat(decorator3NanoTime).hasValueLessThan(decorator2NanoTime.get()); |
| 158 | + |
| 159 | + } |
| 160 | + |
142 | 161 | private static Mono<Void> writeToResponse(ServerWebExchange exchange, String value) {
|
143 | 162 | byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
|
144 | 163 | DataBuffer buffer = DefaultDataBufferFactory.sharedInstance.wrap(bytes);
|
145 | 164 | return exchange.getResponse().writeWith(Flux.just(buffer));
|
146 | 165 | }
|
147 | 166 |
|
| 167 | + @Configuration |
| 168 | + static class HttpHandlerDecoratorBeansConfig { |
| 169 | + |
| 170 | + @Bean |
| 171 | + public WebHandler webHandler() { |
| 172 | + return exchange -> Mono.empty(); |
| 173 | + } |
| 174 | + |
| 175 | + @Bean |
| 176 | + public AtomicLong decorator1NanoTime() { |
| 177 | + return new AtomicLong(); |
| 178 | + } |
| 179 | + |
| 180 | + @Bean |
| 181 | + @Order(1) |
| 182 | + public HttpHandlerDecoratorFactory decorator1() { |
| 183 | + return handler -> { |
| 184 | + decorator1NanoTime().set(System.nanoTime()); |
| 185 | + return handler; |
| 186 | + }; |
| 187 | + } |
| 188 | + |
| 189 | + @Bean |
| 190 | + public AtomicLong decorator2NanoTime() { |
| 191 | + return new AtomicLong(); |
| 192 | + } |
| 193 | + |
| 194 | + @Bean |
| 195 | + @Order(3) |
| 196 | + public HttpHandlerDecoratorFactory decorator2() { |
| 197 | + return handler -> { |
| 198 | + decorator2NanoTime().set(System.nanoTime()); |
| 199 | + return handler; |
| 200 | + }; |
| 201 | + } |
| 202 | + |
| 203 | + @Bean |
| 204 | + public AtomicLong decorator3NanoTime() { |
| 205 | + return new AtomicLong(); |
| 206 | + } |
| 207 | + |
| 208 | + @Bean |
| 209 | + @Order(2) |
| 210 | + public HttpHandlerDecoratorFactory decorator3() { |
| 211 | + return handler -> { |
| 212 | + decorator3NanoTime().set(System.nanoTime()); |
| 213 | + return handler; |
| 214 | + }; |
| 215 | + } |
| 216 | + |
| 217 | + } |
| 218 | + |
148 | 219 |
|
149 | 220 | @Configuration
|
150 | 221 | @SuppressWarnings("unused")
|
|
0 commit comments