TraceWebClientBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String beanName) { if (bean instanceof WebClient) { WebClient webClient = (WebClient) bean; return wrapBuilder(webClient.mutate()).build(); } else if (bean instanceof WebClient.Builder) { WebClient.Builder webClientBuilder = (WebClient.Builder) bean; return wrapBuilder(webClientBuilder); } return bean; } TraceWebClientBeanPostProcessor(ConfigurableApplicationContext springContext); @Override Object postProcessBeforeInitialization(Object bean, String beanName); @Override Object postProcessAfterInitialization(Object bean, String beanName);  }
@Test void should_add_filter_only_once_to_web_client_via_builder() { TraceWebClientBeanPostProcessor processor = new TraceWebClientBeanPostProcessor(this.springContext); WebClient.Builder builder = WebClient.builder(); builder = (WebClient.Builder) processor.postProcessAfterInitialization(builder, "foo"); builder = (WebClient.Builder) processor.postProcessAfterInitialization(builder, "foo"); builder.build().mutate().filters(filters -> { BDDAssertions.then(filters).hasSize(1); BDDAssertions.then(filters.get(0)).isInstanceOf(TraceExchangeFilterFunction.class); }); }